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

hdu1002,大数加法,朴素版

发布时间:2020-12-14 03:31:27 所属栏目:大数据 来源:网络整理
导读:#includeiostream #includecstring #includecstdio using namespace std; const int maxx=1010; char * add(char *s1,char *s2) { ??? int a[maxx+10]={0},b[maxx+10]={0}; ??? int l1=strlen(s1); ??? int l2=strlen(s2); ??? for (int i=l1-1,j=maxx;i=0;i
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int maxx=1010; char * add(char *s1,char *s2) { ??? int a[maxx+10]={0},b[maxx+10]={0}; ??? int l1=strlen(s1); ??? int l2=strlen(s2); ??? for (int i=l1-1,j=maxx;i>=0;i--,j--) ??????? a[j]=s1[i]-'0'; ??? for (int i=l2-1,j--) ??????? b[j]=s2[i]-'0'; ??? for (int i=maxx;i>=0;i--) ??? { ??????? a[i]+=b[i]; ??????? if(a[i]>=10) a[i-1]+=a[i]/10,a[i]%=10; ??? } ??? char s3[maxx+10];?? ?//把int数组转化为char数组,有时可以直接输出 ??? memset(s3,sizeof(s3)); ??? int temp=0; ??? int k=0; ??? for (int i=0;i<=maxx;i++) ??? { ??????? if (a[i]!=0&&temp==0)?? temp=1; ??????? if(temp==1) s3[k++]=a[i]+'0'; ??? } ??? return s3;//返回局部变量的地址,警告 } int main() { ??? char a[1010],b[1010]; ??? int n; ??? while (cin>>n) ??? { ??????? for (int i=1;i<=n;i++) ??????? { ??????????? cin>>a>>b; ??????????? printf("Case %d:n",i); ??????????? cout<<a<<" + "<<b<<" = "; ??????????? cout<<add(a,b)<<endl; ??????????? if (i!=n) ??????????????? cout<<endl; ??????? } ??? } }

(编辑:李大同)

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

    推荐文章
      热点阅读