基本大致流程:
1.先计算,将结果变成string,利用字符串进行输出;
2.变成字符串时变成反序,这样有利于从第一位开始进行三位插入一个逗号;
3.判断正负号进行输出;
注意点:0+0可能会出现测试点问题;
1 #include<iostream> 2 #include<stdlib.h> 3 #include<stdio.h> 4 #include<vector> 5 #include<string> 6 #include<math.h> 7 #include<algorithm> 8 using namespace std; 9 using std::vector; 10 int main() { 11 int a, b; 12 cin >> a >> b; 13 int c = a + b; 14 string s; 15 if (c < 0) { 16 cout << '-'; 17 } 18 c = abs(c); 19 if(c == 0) { 20 cout << 0 << endl; 21 system("pause"); 22 return 0; 23 } 24 int index = 0; 25 while (c != 0) { 26 index++; 27 s.push_back(c % 10+'0'); 28 c = c / 10; 29 if (index % 3 == 0) 30 s.push_back(','); 31 } 32 if (*(s.end() - 1 )== ',') 33 s.pop_back(); 34 reverse(s.begin(), s.end()); 35 cout << s << endl; 36 system("pause"); 37 return 0; 38 }