思想: 运用字符串和整数的转换关系,然后模拟手写运算从后往前依次计算;详细参考【 大数加减乘除】
AC代码;
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <algorithm> #include <sstream> #include <cctype> #include <set> #include <string> #include <stack> #include <vector> using namespace std; const int N = 1050; typedef long long LL; const int inf = 0x3f3f3f3f; LL numA[N]; LL numB[N]; void resetnumA(string numAStr) { memset(numA,0,N*sizeof(LL)); for(int i=0;i<numAStr.length();i++) { numA[i]=numAStr[numAStr.length()-i-1]-'0'; } } void resetnumB(string numBStr) { memset(numB,0,N*sizeof(LL)); for(int i=0;i <numBStr.length();i++) { numB[i]=numBStr[numBStr.length()-i-1]-'0'; } } string getnumString(LL *num) { string numString; int f=0; for(int i=N-1;i>=0;i--) { if(num[i]!=0) f=1; if(f) numString+=num[i]+'0'; } return numString; } string plusn(string numAStr,string numBStr) { resetnumA(numAStr); resetnumB(numBStr); for(int i=0;i<N;i++) { numA[i]=numA[i]+numB[i]; if(numA[i]>9) { numA[i+1]++; numA[i]-=10; } } return getnumString(numA); } int main() { string numAStr,numBStr; int t,y=1; cin>>t; while(t--) { cin>>numAStr>>numBStr; cout<<"Case "<<y++<<":"<<endl; cout<<numAStr<<" + "<<numBStr<<" = "<<plusn(numAStr,numBStr)<<endl; if(t) cout<<endl; } return 0; }