两个数取和,将得到的结果按标准格式输出。
标椎格式:
abs(sum)<1000时,按原先值输出
abs(sum)>=1000时,从个位开始,每三个插入一个 " , "
1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 using namespace std; 7 8 int main() 9 { 10 int num; 11 long long a, b; 12 while (cin >> a >> b) { 13 long long sum = a + b; 14 if (sum == 0|| abs(sum) < 1000) { 15 cout << sum << endl; 16 continue; 17 } 18 num = 1; vector<char> arry; 19 char flag = 0; 20 if (sum < 0) flag = 1; 21 sum = abs(sum); 22 while (sum) { 23 arry.push_back(sum % 10 + '0'); 24 sum /= 10; 25 if (num++ % 3 == 0) arry.push_back(','); 26 } 27 if (num % 3 == 1) arry.pop_back(); 28 if (flag) cout << '-'; 29 for (int i = arry.size() - 1; i >= 0; i--) cout << arry[i]; 30 cout << endl; 31 } 32 return 0; 33 }