---恢复内容开始---
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400
对于该题目,主要在于其输出格式方面,因此考虑到字符数组存储结果,再输出
#include<iostream> #include<cstdlib> using namespace std; int main() { int A, B, C; while (cin >> A >> B) { int flag = 1;//标志结果的正负 C = A + B; if (C < 0) { flag = 0; C = -C;//将其转换为正数,方便后面计算 } int length = -1; char a[10]; int i = 0; while (true) { a[++length] = C % 10 + '0'; i++; C = C / 10; if (C == 0) break; if (i % 3 == 0) a[++length] = ','; } if (flag == 0) cout << "-"; while (length >= 0) { cout << a[length--]; } cout << endl; } system("pause"); return 0; }
由于计算出的结果转换成的形式属于栈的特点,所以使用栈存储输出结果会比较简单些
网上代码:
#include<cstdio> #include<queue> #include<stack> #define QWERTY using namespace std; int main() { long a,b,sum; scanf("%ld%ld",&a,&b); sum=a+b; long st=sum; int i=0,c1,c2; if(st<1000&&st>-1000) { printf("%ld ",st); return 0; } if(st<0) { printf("-"); st=-st; } stack<int> sta; while(st>0) { i++; sta.push(st%10); st=st/10; } #ifdef QWERTY while(!sta.empty()) { i--; printf("%d",sta.top()); if(i!=0&&i%3==0) printf(","); sta.pop(); } #endif printf(" "); return 0; }