PAT-ADVANCED-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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

将a+b的和从低到高,三位的隔开.

在这里我们直接使用stack来存储,需要注意res=0的情况;输出第一个和剩下来的补全问题

#include <bits/stdc++.h>
#define LL long long
#define MAXN 100000+50
#define MOD 1000000007
using namespace std;

stack<int> s;
int  main(){
    int res, a, b;
    scanf("%d%d", &a, &b);
    res = a+b;
    if(!res){
        printf("0");
        return 0;
    }
    if(res < 0){
        printf("-");
        res = -res;
    }
    while(res){
        s.push(res%1000);
        res /= 1000;
    }
    bool flag = false;
    while(!s.empty()){
        if(!flag){
            printf("%d", s.top());
            s.pop();
            flag = true;
        }
        else{
            printf(",%03d", s.top());
            s.pop();
        }
    }
    return 0;
}

CAPOUIS‘CODE

时间: 2024-10-27 02:33:49

PAT-ADVANCED-1001-A+B Format的相关文章

【PAT】1001. A+B Format (20)

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 Each input file contains one test case. Each case cont

PAT 甲级 1001 A+B Format (20)(20 分)

1001 A+B Format (20)(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 Each input file contains one test case. Each case

PAT Advanced 1001

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.

PAT甲级 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 Each input file contains one test case. Each case contains a pair of i

PAT 甲级 1001 A+B Format

https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 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). Inpu

pat甲级 1001 A+B Format

要写甲级题,首要任务是解决英文这个大难题. 困难词汇(我不认识的):calculate计算  standard format 标准格式  digits数字  separated 分离  commas逗号 这道题的大致意思是,给出两个数a和b,并且a和b都大于等于-10的6次方小于等于10的6次方,求出a和b的和,将这个和,每三个数字用逗号分隔一下.看懂了题意以后这题就简单了 ac代码如下: #include <iostream> #include<string> #include&

PAT甲级——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) Sample Input: -1000000 9 Sample Output: -999,991 第一种方法,注意题目说明的数字范围,及时处理越界即可. 为啥捏,因为 in

PAT:1001. A+B Format (20)(数组存储,逢三加“,”) AC

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } int arr[30]; int i=0; do { arr[i]=sum%10; sum/=10; ++i; }while(sum!=0); for(int j=i-1 ; j>=0 ; --j) { pr

PAT:1001. A+B Format (20)(取整取余法) 部分正确

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum>1000000) //[思维],两个数字在:-1000000 <= a, b <= 1000000,之和不会超过1000000再加三个0的数量级 printf("%d,%d

PAT:1001. A+B Format (20) AC

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum>=1000000) //[思维],两个数字在:-1000000 <= a, b <= 1000000,之和不会超过1000000再加三个0的数量级 printf("%d,%