题目描述
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).
输入描述
Each input file contains one test case. Each case contains a pair of integers a and b where −106 ≤ a,b ≤ 106. The numbers are separated by a space.
输出描述
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.
样例
-1000000 9
-999,991
思路
题目就是让按照外国那种3位一个,的方式输出两个数相加的结果,然后就自然而然想到了manacher里插字符的骚操作,所以我干脆也插字符了
注意:-,999,991
是错的(不用想也知道
实现
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a,b,cnt,t;
char s[15];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void fun(int x){
int f=0;//判断是否应该加','
if(!x){
putchar('0');
return;
}
if(x<0) putchar('-'),x=-x;
while(x){
if(t%3==0&&f) s[cnt++]=',';
s[cnt++]=x%10+'0';
t++;//和cnt区分开,防止','多占位数
x/=10;
f=1;
}
for(int i=cnt-1;i>=0;i--){
putchar(s[i]);
}
}
int main(){
a=read(),b=read();
fun(a+b);
return 0;
}