PAT Advanced level 1001 A+B Format

Posted wypx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT Advanced level 1001 A+B Format相关的知识,希望对你有一定的参考价值。

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



虽然放假了但是并不能愉快划水qwq
嗯,因为作死报了个PAT于是现在开始刷PAT的题了。。。
希望开学前能刷完吧(大概

第一题,原来a+b还能出成这样。。。
计算一下数字位数,然后一位一位输出,每三位加逗号
注意正负号以及和为0的情况

#include<iostream>
#include<cstdio>
using namespace std;
void output(int x){
    if(!x){printf("0");return;}
    int sign=(x<0?-1:1);
    int len=0,a[10];
    while(x){
        a[len]=x%10;
        ++len;
        x/=10;
    }
    if(sign<0)printf("-");
    for(int i=len-1;i>=0;--i){
        printf("%d",sign*a[i]);
        if(i%3==0 && i)printf(",");
    }
}
int main(){
    int a,b;
    scanf("%d%d",&a,&b);
    output(a+b);
    return 0;
} 

 

 

以上是关于PAT Advanced level 1001 A+B Format的主要内容,如果未能解决你的问题,请参考以下文章

浙大 PAT Advanced level 1026. Table Tennis (30)

PAT Advanced 1001

1093. Count PAT's (25)计数——PAT (Advanced Level) Practise

PAT (Advanced Level) 1029. Median (25)

PAT (Advanced Level) 1032. Sharing (25)

1078. Hashing (25)Hash + 探测——PAT (Advanced Level) Practise