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 Input1-1000000 9
-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内容请搜索靠谱客的其他文章。
发表评论 取消回复