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 分)

上一篇:数据分析复盘报告,用Excel轻松搞掂!


下一篇:List接口中的常用方法