PTA (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 ?106≤a,b≤106. 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
思路
  • 将数字转换为字符串,每隔3个输出一个,,要注意的点写在代码注释里了
代码
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a,b;
    cin >> a >> b;
    int ans = a + b;
    if(ans / 1000 == 0)     //只有3位
        cout << ans;
    else
    {
        vector<char> v;
        int cnt = 0;
        int t;
        char tmp;
        if(ans < 0)
         {
            cout << '-';
            ans = -ans;
         }
        while(ans)
        {
            t = ans % 10;
             tmp = t + '0';
            v.push_back(tmp);
            ans /= 10;
            cnt++;
            if(cnt == 3 && ans)     //为了避免输出形如-,999,991这样的答案
            {
                v.push_back(',');
                cnt = 0;
            }
        }
        for(int i=v.size()-1;i>=0;i--)
            cout << v[i];
    }
    return 0;
}

引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400

原文地址:https://www.cnblogs.com/MartinLwx/p/11706810.html

时间: 2024-08-30 05:31:43

PTA (Advanced Level)1001.A+B Format的相关文章

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 pa

PAT (Advanced Level) 1001. A+B Format (20)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; int a,b,tot,cnt; int u[20]; char ans[100]; int main() { while(~scanf("%d%d",&a,&b)) { a=a+b

PTA(Advanced Level)1036.Boys vs Girls

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contains a positive integer N, f

PTA(Advanced Level)1025.PAT Ranking

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by e

PTA (Advanced Level)1018 Public Bike Management

Public Bike Management There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Manag

PTA (Advanced Level)1066 Root of AVL Tree

Root of AVL Tree An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. F

PTA (Advanced Level)1022 Digital Library

Digital Library A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a r

PTA (Advanced Level)1023 Have Fun with Numbers

Have Fun with Numbers Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers

PTA(Advanced Level)1011.World Cup Betting

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their