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

【题意】

输入两个整数a,b然后让其相加并将其的和每3个用“,”隔开

【注意】

1.这个逗号隔开的形式是比如 :   1111+1111 那么应该输出的结果是2,222而不是222,2(因为我起初这么想然后错了........)

2.还要注意形如 :

  • 111,
  • ,111
  • 111,

     

 1 #include<iostream>
 2 #include<cmath>
 3 #include<stack>
 4 using namespace std;
 5 int main() {
 6     int a,b;
 7     while (cin >> a >> b) {
 8         int ans = a + b;
 9
10         //处理负号
11         if (ans < 0) cout << "-";
12         ans = abs(ans);
13
14         //用栈来存储
15         stack<int> s;
16         int cnt=0;
17
18         //如果ans是0就直接输出了吧
19         if (ans == 0) {
20             cout << 0 << endl;
21             continue;
22         }
23
24         while (ans) {
25             s.push(ans % 10);
26             ans /= 10;
27         }
28         int mk;   //标记前面第几个digit会有逗号用的,因为这边不一定是第三个
29         int array[100000];
30         while (!s.empty()) {
31             array[++cnt] = s.top();
32             s.pop();
33         }
34
35         mk = cnt % 3;
36         for (int i = 1; i<=cnt; ++i) {
37             //为了防止最后会多出个逗号就这么来了,本来还想再用个cnt2来判断
38             if ((i-mk-1>0)&&(!((i-mk-1)%3))) cout<<",";
39             else if (i-1==mk&&i-1>0)cout<<",";
40             cout << array[i];
41         }
42         cout << endl;
43     }
44 }

原文地址:https://www.cnblogs.com/drake233/p/10191532.html

时间: 2024-11-08 06:27:22

PAT甲级1001的相关文章

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

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

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)

题目原文: 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水题飘过

1 #include<iostream> 2 using namespace std; 3 4 int main(){ 5 int a, b; 6 while(scanf("%d%d", &a, &b) != EOF){ 7 int c = a + b; 8 if(c == 0) printf("0\n"); 9 else{ 10 if(c < 0){ 11 printf("-"); 12 c = -c; 13

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 代码实现及相关知识学习

准备参加九年九月份的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). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu