加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

1168:大整数加法

发布时间:2020-12-13 17:33:55 所属栏目:PHP教程 来源:网络整理
导读:传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1168 ? 用字符将其读入,随后倒记在整数数组(一位一个数字)中(避免没地方好进位),加后进位,最后去掉多余的零,OK ? 1 #includeiostream 2 #includecstring 3 #define N 210 4 using namespace

传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1168

?

用字符将其读入,随后倒记在整数数组(一位一个数字)中(避免没地方好进位),加后进位,最后去掉多余的零,OK

?

 1 #include<iostream>
 2 #include<cstring>
 3 #define N 210
 4 using namespace std;
 5 int as[N],bs[N],lena,lenb;
 6 string a,b;
 7 int main(){
 8     cin>>a>>b;
 9     lena=a.size();
10     lenb=b.size();
11     for(int i=0;i<lena;i++)as[i]=a[lena-i-1]-0;
12     for(int i=0;i<lenb;i++)bs[i]=b[lenb-i-1]-0;
13     int length=max(lena,lenb);
14     for(int i=0;i<length;i++)as[i]+=bs[i];
15     for(int i=0;i<length;i++){
16         if(as[i]>9){
17             as[i+1]++;
18             as[i]-=10;
19             if(i==length-1)length++;
20         }
21     }
22     while(length&&as[length]==0)length--;
23     for(int i=length;i>=0;i--)cout<<as[i];
24     cout<<endl;
25 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读