1001. A+B Format (20)
来源:互联网 发布:淘宝 桌游 编辑:程序博客网 时间:2024/06/07 11:57
时间限制
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
#include <iostream>#include <string>#include <stdio.h>using namespace std;int main(){int a,b;cin>>a>>b;char str[20];sprintf(str,"%d",a+b);string raw(str);string output;int count=0;for(int i=raw.size() - 1;i>=0;i--,count++){if(count != 0 &&count % 3 == 0 && raw[i] != '-')output.insert(output.begin(),',');output.insert(output.begin(),raw[i]);}cout<<output<<endl;return 0;}
方法2:
#include <iostream>#include <stdio.h>#include <string>using namespace std;int main(){int a,b;cin>>a>>b;if(a+b<0)cout<<'-';char res[30];sprintf(res,"%d",a+b>0?a+b:-(a+b));string output(res);int chop = output.size()%3;for(int i=0;i<output.size();i++){if( (i-chop)%3 == 0 && i != 0)cout<<',';cout<<output[i];}cout<<endl;system("pause");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)
- springMvc 可传入的参数
- 搭建云端服务器
- linux Centos6.5使用yum安装mysql
- 安装nexus
- dp 0-1
- 1001. A+B Format (20)
- nanomsg的使用与分析
- C++Primer学习之四初始化
- 界面上单元格的可操作性和背景颜色根据某一个单元格值的变化而发生改变
- 总结2015,启航2016
- 使弹出框的标题显示为父窗体上选中行的内容
- 界面元素查询语句计算页数SQL中增加汇总值描述
- 隐马尔可夫模型(HMM)攻略
- 实现界面全选效果