首页 > 其他 > 详细

1047 Integer Inquiry

时间:2018-08-09 12:00:34      阅读:123      评论:0      收藏:0      [点我收藏+]

String 大数加法模板

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;

//高精度加法
//只能是两个正数相加
string add(string str1,string str2)//高精度加法
{
    string str;

    int len1=str1.length();
    int len2=str2.length();
    //前面补0,弄成长度相同
    if(len1<len2)
    {
        for(int i=1;i<=len2-len1;i++)
           str1="0"+str1;
    }
    else
    {
        for(int i=1;i<=len1-len2;i++)
           str2="0"+str2;
    }
    len1=str1.length();
    int cf=0;
    int temp;
    for(int i=len1-1;i>=0;i--)
    {
        temp=str1[i]-0+str2[i]-0+cf;
        cf=temp/10;
        temp%=10;
        str=char(temp+0)+str;
    }
    if(cf!=0)  str=char(cf+0)+str;
    return str;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string sum="0";
        string str1;
        while(cin>>str1)
        {
            if(str1=="0")break;
            sum=add(sum,str1);
        }
        cout<<sum<<endl;
        if(T>0)cout<<endl;
    }
    return 0;

}

 

1047 Integer Inquiry

原文:https://www.cnblogs.com/nr1999/p/9447861.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!