NYIST 844 A+B Problem(V)
来源:互联网 发布:js中的类选择器 编辑:程序博客网 时间:2024/05/17 09:12
今天写这篇文章是要吐槽一下自己,这两天在学习容器,做这题的时候一见两个数相加就顺手用了string,而且在这个过程中还修改了不少。等做完看大神们的代码的时候,瞬间有种淡淡的忧伤~~~好简单啊,为啥当时大脑短路呢~嘿嘿~不过自己倒是对容器了解了一些。
以下附加代码,看完就知道有多简单了。以后在做题的过程中要多想多思考。
先附上string的,由于是初学,用的还不太好,耗时间耗内存,伤不起啊~~~
#include<string>#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int main(){ string a,b; while(cin>>a>>b) { char c[15]; int max,j; if(a.compare("0")==0&&b.compare("0")==0) break; int d1,d2,i; d1=a.length(); d2=b.length(); if(d1!=d2) { if(d1>d2) { for(i=d2+1;i<=d1;i++) b=b+'0'; max=d1; } else { for(i=d1+1;i<=d2;i++) a=a+'0'; max=d2; } } else { max=d1; } reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); int m=0,t1,t2,temp; for(i=max-1;i>=0;i--) { t1=a[i]-'0'; t2=b[i]-'0'; temp=(t1+t2+m)%10; c[i]=temp+'0'; m=(t1+t2+m)/10; } if(m) { cout<<m; for(i=0;i<max;i++) cout<<c[i]; } else { for(i=0;i<max;i++) { if(c[i]!='0') {j=i;break;} } for(i=j;i<max;i++) cout<<c[i]; } cout<<endl; } return 0;}下面是没有用容器取余实现的反转:
#include<cstdio>int reverse(int n){ int m=0; while(n) //实现反转 { m=m*10+n%10; n=n/10; } return m;}int main(){ int x,y,answer; while(scanf("%d%d",&x,&y)&&(x+y)) { answer=reverse(x)+reverse(y); printf("%d\n",answer); } return 0;}
0 0
- NYIST 844 A+B Problem(V)
- nyoj-844-A+B Problem(V)
- 844-A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ-844 A+B Problem(V)
- 844 A+B Problem(V)
- ny 844 A+B Problem(V)
- nyoj 844 A+B Problem(V)
- 南阳844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- NYOJ 844 A+B Problem(V)
- 【NYOJ】[844]A+B Problem(V)
- 题目844 A+B Problem (V)
- NYOJ 844 A+B Problem(V)
- 五个方法成为更好的程序员
- STM32中断—优先级设置
- Android 应用程序建立与WMS服务之间的通信过程
- iOS VC Modal Styles
- C/C++源代码到可执行程序的过程详解
- NYIST 844 A+B Problem(V)
- python yield用法
- 学会编译原理的重要性
- 图像增强个人总结心得
- 关于禁止程序重复启动的另一种需要与实现
- 【数据结构】·【顺序表】
- 关于禁止程序重复启动的另一种需要与实现
- 怎样读项目代码
- Outlook2013添加网易163邮箱设置方法