1001 A+B Format (20 分)
Posted 强风吹拂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1001 A+B Format (20 分)相关的知识,希望对你有一定的参考价值。
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).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
思路:
对两个数的和c进行讨论,先将c映射到正区间,对1000循环取余存入数组,然后倒序输出即可,注意需要填0补位,另外第一个数不能填0补位
#include<bits/stdc++.h> using namespace std; const int maxn=10010; int main(){ int a,b; scanf("%d %d",&a,&b); int c=a+b; if(c<0){ printf("-"); c=-c; } int num[maxn]; int i=0; if(c==0){ printf("%d\\n",0); return 0; } while(c>0){ num[i]=c%1000; c/=1000; i++; } printf("%d",num[i-1]); for(int j=i-2;j>=0;j--){ printf(",%03d",num[j]); } printf("\\n"); return 0; }
以上是关于1001 A+B Format (20 分)的主要内容,如果未能解决你的问题,请参考以下文章