码迷,mamicode.com
首页 > 其他好文 > 详细

PTA乙级 (1074 宇宙无敌加法器 (20分))

时间:2020-01-17 23:23:06      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:mes   names   class   pre   turn   ret   clu   col   cst   

1074 宇宙无敌加法器 (20分)

https://pintia.cn/problem-sets/994805260223102976/problems/994805263297527808

第一次提交测试五没过:如果和为0,去0的时候保证要留下一个0(测试点5)

第二次AC:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 using namespace std;
 8 int main()
 9 {
10     char chr1[25],chr2[25],chr3[25];
11     int a[25]={0},b[25]={0},c[25]={0},d[25]={0};
12     int len;
13     int i=0,j=0,k=0,s=0;
14     cin>>chr1;
15     cin>>chr2>>chr3;
16     int len1=strlen(chr1);
17     int len2=strlen(chr2);
18     int len3=strlen(chr3);
19     if(len2>len3) len=len2;
20     else len=len3;
21     for(i=len1-1;i>=0;i--)
22     {
23         if(chr1[i]!=0) a[j]=chr1[i]-0;
24         else a[j]=10;
25         j++;
26     }
27     for(i=len2-1;i>=0;i--)
28     {
29         b[k]=chr2[i]-0;
30         k++;
31     }
32     for(i=len3-1;i>=0;i--)
33     {
34         c[s]=chr3[i]-0;
35         s++;
36     }
37     int m=0;
38     for(i=0;i<len;i++)
39     {
40         d[i]=(b[i]+c[i]+m)%a[i];
41         m=(b[i]+c[i]+m)/a[i];
42     }
43     if(m>0) d[i]=m;
44     else i-=1;
45     bool flag=false;
46     s=i;
47     for(;s>0;s--)
48     {
49         if(!d[i]&&!flag)
50         {
51             i--;
52         }
53         else if(d[i]) flag=true;
54     }
55     for(s=i;s>=0;s--) cout<<d[s];
56     cout<<endl;
57     return 0;
58 }

PTA乙级 (1074 宇宙无敌加法器 (20分))

标签:mes   names   class   pre   turn   ret   clu   col   cst   

原文地址:https://www.cnblogs.com/jianqiao123/p/12207700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!