大数相加版本的A+B 该问题中的A和B最多可以多到两个千位数相加 此时用纯C风格和JAVA风格的代码解题
1002 A+B PROBLEM 题目Question
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
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.
Output
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. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
case1:
1 + 2 = 3
case2:
112233445566778899 + 998877665544332211 = 1111111111111111110
- 因为a和b很大 最高1000位数 无法使用整形或者长整型将其存下 故先把a和b存到两个字符串中再把a和b的长度获取到 从最后一位往前加存到结果串中 直到a和b中有一个串长度为0把较长的串未加的部分复制到结果串的末尾 如果两者长度一致 且最后一位不为0 则增加一位数方便输出按从后往前的顺序输出结果串不是最后一个Case就多输出一个换行
#include程序的运行结果如图所示 1002 A+B PROBLEM JAVA语言的解题思路#include #include //HDU OJ 1002 A+B PROBLEM //使用字符串储存a串和b串 题目可知最多一千位 故申请1005个字符空间 char a[1005],b[1005]; //使用整型数组储存结果串 更方便处理 //此时 两个一千位数相加位数不可能超过1001位 全部初始化为0 int res[1005]= {0}; //使用la和lb来储存a串与b串的大数 int la=0,lb=0; //使用p来存储结果的位数 int p = 0; int flag = 0; void sum(char a[1005],char b[1005]) { la = strlen(a); lb = strlen(b); p = 0; //使用memset函数填充res数组 memset(res,0,sizeof(res)); int j=la-1; int k=lb-1; //逆序相加 先加 a串b串中最后的数 结果顺序存入res中 直到a的未运算的长度为0 或者 b的未运算的长度为0 while(j>=0&&k>=0) { //如果res[p]和a,b最后一个未加的位数相加大于10 则res[p+1]的数+1 进一位 if(res[p]+(a[j]-'0')+(b[k]-'0')>=10) { res[p]=res[p]+(a[j]-'0')+(b[k]-'0')-10; res[p+1]++; } //如果res[p]和a,b最后一个未加的位数相加不大于10 则不会进一位 else { res[p]=res[p]+(a[j]-'0')+(b[k]-'0'); } //res已经运算的位数+1 j a的未做运算的位数-1 k b的未做运算的位数-1 p++; j--; k--; } //如果此时j等于或者大于0 则表明a串位数大于b串 则把a串剩下的位数加到res中 反正 则把b串剩下的位数加到res中 //如果 j 和 k 都小于0 则 查看res[p]是不是不是0 如果不是0 则res的位数p+1; if(j>=0) { for(int t=j; t>=0; t--) { res[p]=res[p]+(a[t]-'0'); p++; } } else if(k>=0) { for(int t=k; t>=0; t--) { res[p]=res[p]+(b[t]-'0'); p++; } } else if(res[p]!=0) { p++; } } int main() { int n; scanf("%d",&n); for(int i = 1 ; i <= n ; i ++ ) { scanf("%s%s",a,b); sum(a,b); printf("Case %d:n",i); printf("%s + %s = ",a,b); //从p-1位向后 逆序输出 for(int t=p-1; t>=0; t--) { printf("%d",res[t]); } printf("n"); if(i!=n){ printf("n"); } } return 0; }
- 因为a和b很大 最高1000位数 无法使用整形或者长整型将其存下JAVA中提供了关于大数的整数BigInteger 和关于大数的小数BigDecimal 该问题适合使用BigInteger来解决使用Scanner.nextBigInteger()接受从控制台输入的大数使用BigDecimal.add()方法把两个数加一起使用BigDecimal.toString()方法输出大数不是最后一个Case就多输出一个换行
import java.math.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger a, b;
Scanner sc = new Scanner(System.in);
int n;
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
a = sc.nextBigInteger();
b = sc.nextBigInteger();
System.out.println("Case " + i);
System.out.println(a.toString() + " + " + b.toString() + " = " + a.add(b).toString());
if(i!=n){
System.out.println("");
}
}
sc.close();
}
}
程序的运行结果如图所示



