hdu1002 A + B Problem II
2017-07-09 22:32
429 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
题意:a+b,高精度加法
解析:c++用数组模拟加法,java就用自带的BigInteger
C++代码
Java代码
题意:a+b,高精度加法
解析:c++用数组模拟加法,java就用自带的BigInteger
C++代码
#include <bits/stdc++.h> using namespace std; const int maxn = 1205; int a[maxn],b[maxn]; int c[maxn]; int main(void) { int t,case_t = 1; scanf("%d",&t); while(t--) { if(case_t!=1) puts(""); string t1,t2; cin>>t1>>t2; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); a[0] = t1.length(),b[0] = t2.length(); for(int i=1;i<=a[0];i++) a[i] = t1[a[0]-i] - '0'; for(int i=1;i<=b[0];i++) b[i] = t2[b[0]-i] - '0'; int lenc = 1,x = 0; while(lenc <= a[0] || lenc <= b[0]) { c[lenc] = b[lenc]+a[lenc]+x; x = c[lenc]/10; c[lenc] %= 10; lenc++; } c[lenc]=x; while(c[lenc]==0 && lenc>1) lenc--; printf("Case %d:\n",case_t++); cout<<t1<<" + "<<t2<<" = "; for(int i=lenc;i>=1;i--) printf("%d",c[i]); puts(""); } return 0; } /* 000 0000 9999 1 1 9999 99900 00999 */
Java代码
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(); for(int i=1;i<=n;i++) { BigInteger a = cin.nextBigInteger(); BigInteger b = cin.nextBigInteger(); BigInteger ans = a.add(b); System.out.println("Case " + i + ":"); System.out.print(a + " + " + b + " = "); System.out.println(ans); if(i!=n) System.out.println(""); } cin.close(); } }
相关文章推荐
- HDU 1002 A + B Problem II
- [ACM]hdu 1002 A + B Problem II (复习大数相加)
- hdu1002 A + B Problem II
- hdu 1002 A+B problem II
- HDU 1002 A + B Problem II
- hdu1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- [hdu 1002] A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu1002 大大大整数加法 A + B Problem II
- hdu 1002.A + B Problem II
- hdu 1002 A+B Problem II
- HDU 1002 A + B Problem II
- HDU1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU_1002 A + B Problem II