做题思路:
简单模拟题,正负数分类讨论,注意不要在负号前加逗号就行。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a, b, len;
string ans;
int main()
{
scanf("%d%d", &a, &b);
ans = to_string(a + b);
len = ans.length();
for(int i = 0; i < len; i ++ )
{
if(ans[0] == '-' && (len - i) % 3 == 0 && i != 1 && i) cout << ",";
else if(ans[0] != '-' && (len - i) % 3 == 0 && i) cout << ",";
cout << ans[i];
}
}
/*
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);
*/