标签:mat ios NPU ESS a+b 使用 group input rate
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)
Sample Input:
-1000000 9
Sample Output:
-999,991
第一种方法,注意题目说明的数字范围,及时处理越界即可。
为啥捏,因为 int 是32位的,最大2的31次方。题目给的数据容易越界
第二种方法,使用to_string
将A+B的值转化为字符串,按需要的格式进行修改。
string s = to_string(a + b);
to_string的用法推荐查阅官方文档,表述清晰
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Convert numerical value to string
Returns a string with the representation of val.
第二种方法较为简单清晰。
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
string s = to_string(a + b);
int len = s.length();
for (int i = 0; i < len; i++) {
cout << s[i];
if (s[i] == '-')
continue;
if ((i + 1) % 3 == len % 3 && i != len - 1)
cout << ",";
}
return 0;
}
标签:mat ios NPU ESS a+b 使用 group input rate
原文地址:https://www.cnblogs.com/mrcangye/p/12231740.html