大数运算 A + B Problem II
Posted jason66661010
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数运算 A + B Problem II相关的知识,希望对你有一定的参考价值。
http://acm.hdu.edu.cn/showproblem.php?pid=1002
代码
#include<iostream> #include<string> #include<cstring> #include<algorithm> struct bignum { int list[2000]; int len; bignum() { memset(list, 0, sizeof(list)); len =0; } }aa,bb,ans; using namespace std; bignum change(string a) { bignum tmp; tmp.len = a.length(); for (int i = 0; i < a.length(); i++) tmp.list[i] = a[tmp.len - i-1]-‘0‘; return tmp; } bignum add(bignum a, bignum b) { int t = 0,i; bignum ans; ans.len = a.len > b.len ? a.len : b.len; for ( i = 0; i < a.len ||i< b.len; i++) { ans.list[i] = (t + a.list[i] + b.list[i]) % 10; t= (t + a.list[i] + b.list[i]) / 10; } if (ans.list[i] > 10) { ans.list[i] = ans.list[i] % 10; ans.list[i + 1] /= 10; } return ans; } int main() { int n; string a, b; scanf("%d", &n); for (int k = 1; k <=n;k++) { printf("Case %d: ", k); cin >> a >> b; cout << a << " + " << b << " = "; aa = change(a); bb = change(b); ans = add(aa, bb); for (int i = ans.len-1; i >= 0;i--) cout << ans.list[i]; cout << endl; if (k != n)cout << endl; } }
以上是关于大数运算 A + B Problem II的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1002: A + B Problem II (大数加法)
题解报告:hdu 1002 A + B Problem II(大数加法)