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

第一种方法,注意题目说明的数字范围,及时处理越界即可。
为啥捏,因为 int 是32位的,最大2的31次方。题目给的数据容易越界

第二种方法,使用to_string将A+B的值转化为字符串,按需要的格式进行修改。
string s = to_string(a + b);
to_string的用法推荐查阅官方文档,表述清晰

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Convert numerical value to string
Returns a string with the representation of val.

第二种方法较为简单清晰。

#include <iostream>
using namespace std;
int main() {
        int a, b;
        cin >> a >> b;
        string s = to_string(a + b);
        int len = s.length();
        for (int i = 0; i < len; i++) {
                cout << s[i];
                if (s[i] == '-')
                        continue;
                if ((i + 1) % 3 == len % 3 && i != len - 1)
                         cout << ",";
        }
        return 0;
}

原文地址:https://www.cnblogs.com/mrcangye/p/12231740.html

时间: 2024-10-03 04:24:40

PAT甲级——1001 A+B Format (20分)的相关文章

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甲级 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 (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 甲级 1027 Colors in Mars (20 分)(简单,进制转换)

1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the

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

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甲级】1042 Shuffling Machine (20 分)

题意: 输入洗牌次数K(<=20),输入54张牌每次洗入的位置(不是交换的位置),输出洗好的牌. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;multiset<int>st;int a[57];int card[57],b[57];int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

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