1001. A+B Format (20)
来源:互联网 发布:gson 源码 编辑:程序博客网 时间:2024/06/06 01:41
1001. A+B Format (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input
Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.
Output
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input-1000000 9Sample Output
-999,991
分析:看到这题感觉简单,就按着思路写程序。刚开始时还没注意到那个逗号(英文不好T_T),起初写的是三个数、三个数的存取,sorry,只通过了几个测试点。接着网查了下,数据应该逐个存取,改过来继续。。。刚接触了vector,感觉很好用,数据存在vector中。
- #include<iostream>
- #include<cmath>
- #include<vector>
- using namespace std;
- int main()
- {
- int A,B,sum=0,temp;
- vector<int> s;
- vector<int>::iterator it;
- int flag=0;
- //freopen("C:\\Documents and Settings\\Administrator\\桌面\\cin.txt","r",stdin); //数据输入路径,调试程序方便
- cin>>A>>B;
- sum=A+B;
- if(sum==0){
- cout<<"0"<<endl;
- return 0;
- }
- if(sum<0)
- cout<<"-";
- sum=abs(sum);
- while(sum!=0){//将sum逐个分开
- temp=sum%10;
- s.push_back(temp);
- sum=sum/10;
- }
- flag=3-(s.size()%3);//flag,逗号输出标记
- for(it=s.end()-1;it!=s.begin();it--){
- if(!s.empty()){
- cout<<(*it);
- flag++;
- if(flag%3==0)
- cout<<",";
- }
- }
- cout<<(*it)<<endl;
- return 0;
- }
0 0
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- 1001. A+B Format (20)
- UVa 12333 – Revenge of Fibonacci [大数+字典树]
- vc6 弹出对话框添加菜单,添加256真彩色工具栏
- Leetcode--Find Minimum in Rotated Sorted Array
- 插入排序
- oracle的常用函数 instr() 和substr()函数
- 1001. A+B Format (20)
- 最小生成树-Prim算法和Kruskal算法
- Gson解析josn数据的2种方法
- 上传视频时自动完成截取缩略图(二)
- C#中静态与非静态方法比较
- 在Ubuntu 12.04安装和设置Samba实现网上邻居共享
- Epoll在LT和ET模式下的读写方式
- iOS 之AutoReleasePool陷阱
- hdu-5057(离线的树状数组)