版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/88081893
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
人生苦短,我用(xiang)Py。我用C++写的一个只有14分。。。。
a,b = map(int,input().split())
print(format(a+b,','))
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
int sum = a + b;
string str = to_string(sum);
stack<char> s; //利用栈“先进后出”这个性质
//将字符串中的字符全部推入栈中
for(auto it : str)
{
s.push(it);
}
string result = "";
int cnt = 0;
while(!s.empty()) //清仓大甩卖
{
cnt++;
result = s.top() + result;
s.pop();
if(cnt == 3) //每三位数加一个逗号
{
result = "," + result;
}
}
cout << result << endl;
return 0;
}