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
1001.格式化A+B
计算a+b,并以如下标准的格式输出二者的和:即输出的数字必须每三个一组用逗号分开(当然少于四个数字的除外)。
输入
每次输入一个测试用例。每个测试用例包括一对整数a和b,且-1000000<=a,b<=1000000,a和b之间有空格。
输出
对于每一个测试用例,在一行中输出a和b的和。且和必须以标准的格式输出。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 1000001;
int main()
{
int a, b;
scanf("%d %d", &a, &b);
int c = 0;
c = a + b;
char str[MAX];
sprintf(str, "%d", c);
int len = strlen(str);
if(str[0] == '-'){
if(len <= 4){
printf("%s\n", str);
}
else{
printf("-");
int con = 0;
int d = (len-1)%3;
int i = 1;
for(i=1; i<d+1; i++){
printf("%c", str[i]);
}
if(d != 0)
printf(",");
for(i; i<len; i++){
printf("%c", str[i]);
con++;
if(con == 3){
if(i == len-1)
break;
else{
printf(",");
con = 0;
}
}
}
printf("\n");
}
}
else{
if(len <= 3){
printf("%s", str);
}
else{
int con = 0;
int d = len%3;
int i = 0;
for(i=0; i<d; i++){
printf("%c", str[i]);
}
if(d != 0)
printf(",");
for(i; i<len; i++){
printf("%c", str[i]);
con++;
if(con == 3){
if(i == len-1)
break;
else{
printf(",");
con = 0;
}
}
}
printf("\n");
}
}
return 0;
}