概述
1001. A+B Format (20)
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<cstdio>
#include<vector>
using namespace std;
vector<int> List;
int main(){
int a, b, sum;
int flag=0, digit;
int i;
scanf("%d %d", &a, &b);
sum = a + b;
if (sum == 0){
printf("%d", sum);
return 0;
}
if (sum < 0){
sum = -sum;
flag = 1;
}
while (sum>0){
digit = sum % 10;
List.push_back(digit);
sum = sum / 10;
}
if (flag == 1) printf("-");
for (i = List.size()-1; i>=0; i--){
printf("%d", List[i]);
if (i % 3 == 0 && i != 0) printf(",");
}
return 0;
}
最后
以上就是火星上树叶为你收集整理的1001 1001. A+B Format (20)的全部内容,希望文章能够帮你解决1001 1001. A+B Format (20)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复