A+B Problem II 大数加法

发布时间:2020-10-21 整理:编程之家
编程之家收集整理的这篇文章主要介绍了A+B Problem II 大数加法编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随编程之家小编过来看看吧!

A+B Problem II

http://acm.nyist.net/JudgeOnline/problem.php?pid=103

时间限制:3000 ms  |  内存限制:65535 KB

难度:3

输入

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow,each line consists of two positive integers,A and B. Notice that the integers are very large,that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

输出

For each test case,you should output two lines. The first line is "Case #:",# means the number of the test case. The second line is the an equation "A + B = Sum",Sum means the result of A + B. Note there are some spaces int the equation.

样例输入

2
1 2
112233445566778899 998877665544332211

样例输出

Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

描述

I have a very simple problem for you. Given two integers A and B,your job is to calculate the Sum of A + B.

A,B must be positive.

 

我的代码

#include <stdio.h>
#define MAX 1010
char a[MAX],b[MAX],c[MAX]; 
int main(int argc,char *argv[])
{
	int T,t,d,i,j,x,y,N=0;
	scanf("%d",&T);
	while(T--){
		for(i=0;i<MAX;i++)
			a[i]=b[i]=c[i]='0';
		//将字符串改为 数字低位在数组的低位 
		scanf("%s",c);
		for(x=0;c[x];x++);
		for(i=0;i<x;i++)
			a[i]=c[x-1-i];
		a[i]='0';
		
		scanf("%s",c);
		for(x=0;c[x];x++);
		for(i=0;i<x;i++)
			b[i]=c[x-1-i];
		b[i]='0';
		for(i=0;i<MAX;i++)
			c[i]='0';
		c[i]='0';
		
		//加法运算
		d=0; //进位 
		for(i=0;i<MAX;i++){
			t = a[i]+b[i]-2*'0'+d;
			if(t>9){
				d=1;
				c[i]=t%10+'0';
			}else{
				d=0;
				c[i]=t+'0';
			}		
		}
		
		//输出
		printf("Case %d:\n",++N);	 
		//考虑 0+0=0 
		for(i=MAX-1;a[i]=='0'&& i;i--);
		for(;i>=0;i--)
			printf("%c",a[i]);
		printf(" + ");
		for(i=MAX-1;b[i]=='0'&& i;i--);
		for(;i>=0;i--)
			printf("%c",b[i]);	
		printf(" = ");	
		for(i=MAX-1;c[i]=='0' && i;i--);
		for(;i>=0;i--)
			printf("%c",c[i]);
		printf("\n");
	}	
	return 0;
}

 

简单做法

#include <stdio.h>
#include <string.h>
#define MAX 1020
char a[MAX],char *argv[])
{
	int n=0,T,lena,lenb,lenc;	
	scanf("%d",&T);
	while(T--){
		scanf("%s",a);
		scanf("%s",b);
		lena=strlen(a);
		lenb=strlen(b);
		printf("Case %d:\n",++n);
		printf("%s + %s = ",a,b);			
		d=lenc=0;
		i=lena-1;
		j=lenb-1;
		while(i>=0 && j>=0){
			t=a[i--]+b[j--]-2*'0'+d;
			if(t>9){
				d=1;
				c[lenc++]=t%10;
			}else{
				d=0;
				c[lenc++]=t;
			} 
		}
		while(i>=0){
			t=a[i--]-'0'+d;
			if(t>9){
				d=1;
				c[lenc++]=t%10;
			}else{
				d=0;
				c[lenc++]=t;
			} 	
		}
		
		while(j>=0){
			t=b[j--]-'0'+d;
			if(t>9){
				d=1;
				c[lenc++]=t%10;
			}else{
				d=0;
				c[lenc++]=t;
			} 
		}
		if(d)
			c[lenc++]=1;
		
		for(t=lenc-1;t>=0;t--)
			printf("%d",c[t]);
		printf("\n");
	}
	return 0;
}

 

题目推荐

import java.math.BigInteger;
import java.util.Scanner;
public class Main{

    public static void main(String args[]) {
       Scanner cin=new Scanner(System.in);
       int n=cin.nextInt();
       BigInteger a,b;
       for(int i=1;i<=n;i++){
    	   a=cin.nextBigInteger();
    	   b=cin.nextBigInteger();
    	   System.out.println("Case "+i+":");
    	   System.out.println(a.toString()+" + "+b.toString()+" = "+a.add(b));
       }
    }
}

总结

以上是编程之家为你收集整理的A+B Problem II 大数加法全部内容,希望文章能够帮你解决A+B Problem II 大数加法所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478
编程之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!