PAT甲级1001

作者: minibanana丶 | 来源:发表于2020-04-02 09:46 被阅读0次
写个博客来记录一下自己的甲级刷题,我会按每个知识点分类来刷题,每题会标注是什么类别的题目。

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 −10
​6
​​ ≤a,b≤10
​6
​​ . 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

#include <iostream>
using namespace std;
int main(){
    int a, b;
    cin >> a >> b;
    int c = a + b;
    string sum = to_string(c);
    string res;
    for (int i = sum.size() - 1, j = 0; i >= 0; i--){
        res = sum[i] + res;
        j++;
        if (j % 3 == 0 && sum[i - 1] != '-' && i != 0) res = ',' + res;
    }
    cout << res << endl;
    return 0;
}

这题主要是考查字符串的处理,其中最主要用到的就是to_string()函数,这个函数可以将数字转换为字符串。转换成字符串后就很简单了,按题目给出的样例输出一下即可。

相关文章

  • PAT A1001 A+B Format (20)

    PAT A1001 A+B Format 原题链接PAT甲级题目目录(简书)PAT甲级题目目录(CSDN)在CSD...

  • Pat 甲级 1001

    太感人终于把1001做对了,一开始没有看清题目要求。产生各种各样的错误,最后又有编译错误。在Pat中Java的类名...

  • PAT甲级1001

    写个博客来记录一下自己的甲级刷题,我会按每个知识点分类来刷题,每题会标注是什么类别的题目。 1001 A+B Fo...

  • PAT甲级1001-Python

    1001 A+B Format Calculate a+b and output the sum in stand...

  • PAT甲级 1001 A+B Format

    原题链接 1001 A+B Format 【题目大意】给定两个数a和b, 按照标准格式输出 a + b 的结果。 ...

  • PAT甲级(1001:A+B Format)

    计算a+b并以标准格式输出和——也就是说,数字必须用逗号分隔成三组(除非数字个数少于四位)。 输入规格: 每个输入...

  • 字符串输入问题

    PAT 甲级 1100 People on Mars count their numbers with base ...

  • PAT甲级题解 全部 JAVA版 持续更新

      临近过年,闲来无事。恰逢弟弟准备考PAT甲级,总是问我一些问题。又见网上PAT甲级多为c/c++版本的答案。罕...

  • PAT甲级(Advanced Level)练习题——1001

    从今天开始刷题 =。=会经常更新的 题目描述Given N rational numbers in the fo...

  • Pat甲级 1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format -- th...

网友评论

    本文标题:PAT甲级1001

    本文链接:https://www.haomeiwen.com/subject/gnutphtx.html