A + B Problem II高精度加法(hdu1002)

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/83239704

 

                             A + B Problem II

高精度加法AC模板:

(这个题输出要控制好格式,两个测试案例之间空一行,最后一个案例换行就行)

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 10000
#define Max 0x3f3f3f3f
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn = 2e3 + 5;
int main(){
    ios::sync_with_stdio(false);
    int T;
    //freopen("c1.txt", "r", stdin);
    //freopen("c2.txt", "w", stdout);
    cin>>T;
    int index = 1 ;
    while(T--){
        string s1,s2;
        int num[1005];
        cin>>s1>>s2;
        mst(num);
        int len1 = s1.length(), len2 = s2.length();
        int len = 0, temp = 0;
        for(; len1 > 0 || len2 > 0; len1--, len2--){
            if(len1 > 0 && len2 > 0){
                num[len] = ( (s1[len1 - 1] - '0')+ (s2[len2 - 1] - '0') + temp) % 10;   //本位和
                temp = ( (s1[len1 - 1] - '0') + (s2[len2 - 1] - '0') + temp) / 10;      //向高位的进位
            }
            else if(len1 <= 0){                 //类似与100000 + 100,到高位只能加一个数了 
                num[len] = (s2[len2 - 1] - '0' + temp) % 10;
                temp = ( (s2[len2 - 1] - '0') + temp) / 10;
            }
            else{
                num[len] = (s1[len1 - 1] - '0' + temp) % 10;
                temp = ( (s1[len1 - 1] - '0') + temp) / 10;
            }
            len++;
        }
        cout<<"Case "<<index++<<":"<<endl<<s1<<" + "<<s2<<" = ";
        if(temp > 0){       //最后还有向高位的进位,这里很容易忽视然后WA
            printf("1");
        }
        for(int i = len - 1; i >= 0; i--){
            cout<<num[i];
        }
        if(T != 0){
            cout<<endl<<endl;
        }
        else{
            cout<<endl;
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/83239704
今日推荐