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

注意几个输出点:-991  991 1001 -1001
 1 #include<iostream>
 2 #include <sstream>
 3 #include<string>
 4 #include<stack>
 5 #include<typeinfo>
 6
 7 using namespace std;
 8
 9 int main()
10 {
11   int a,b,flag=0,temp=0;
12
13   cin>>a>>b;
14
15   a+=b;
16
17   if(a<0)
18   {
19       flag=1;
20       cout<<"-";
21   }
22
23   stringstream ss;
24   ss<<a;
25
26   string str=ss.str();
27
28   stack<char> s;
29
30   for(int i=str.length()-1;i>=flag;--i)
31   {
32       s.push(str[i]);
33       ++temp;
34
35       if(temp%3==0 && i-1>=flag)
36           s.push(‘,‘);
37   }
38
39   while(!s.empty())
40   {
41       cout<<s.top();
42       s.pop();
43   }
44
45   cout<<endl;
46
47   return 0;
48 }

原文地址:https://www.cnblogs.com/cdp1591652208/p/9354123.html

时间: 2024-10-10 07:59:02

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

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分)

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

要写甲级题,首要任务是解决英文这个大难题. 困难词汇(我不认识的):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甲级1001

[题目] 1001 A+B Format (20 point(s)) 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

PAT甲级 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)

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)(数组存储,逢三加“,”) 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