首页 > 其他分享 >PAT 甲级 1001 A+B Format

PAT 甲级 1001 A+B Format

时间:2023-02-18 18:55:24浏览次数:40  
标签:case PAT cout Format int sum include 1001

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

Experiential Summing-up 

把 a+b 转化为字符串,除第一位为负号时(特判),当 (数组下标+1)%3 == 总和位数%3 ,且遍历的此数不是最后一位时,输出“,”。

Accepted Code

 1 /*单纯的笨办法*/
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a, b, sum;
 8     cin >> a >> b;
 9     sum = a + b;
10     if(sum < 0)
11     {
12         sum = -sum;
13         cout << "-" ;
14     }
15     if(sum == 0)
16     {
17         cout << 0;
18         return 0;
19     }
20 
21     int count = 0, temp = sum, temp_t = sum;//count为sum的位数
22     while(temp_t != 0)
23     {
24         temp_t /= 10;
25         count ++;
26     }
27 
28     int q[10], ct = count;
29     while(temp != 0)
30     {
31         int g = temp % 10;
32         q[count] = g;
33         temp /= 10;
34         count --;
35     }
36 
37     if(sum < 100 && sum > -100)
38     {
39         for(int i = 1; i <= ct; i ++)
40             cout << q[i];
41         return 0;
42     }
43     if(ct % 3 == 0)
44     {
45         int k = 0;
46         for(int i = 1; i <= ct; i ++)
47         {
48             if(k == 3)
49             {
50                 cout << ",";
51                 k = 0;
52             }
53             cout << q[i];
54             k ++;
55         }
56     }
57     if(ct % 3 == 1)
58     {
59         cout << q[1] << ",";
60         int k = 0;
61         for(int i = 2; i <= ct; i ++)
62         {
63             if(k == 3)
64             {
65                 cout << ",";
66                 k = 0;
67             }
68             cout << q[i];
69             k ++;
70         }
71     }
72     if(ct % 3 == 2)
73     {
74         cout << q[1] << q[2] << ",";
75         int k = 0;
76         for(int i = 3; i <= ct; i ++)
77         {
78             if(k == 3)
79             {
80                 cout << ",";
81                 k = 0;
82             }
83             cout << q[i];
84             k ++;
85         }
86     }
87 
88     return 0;
89 }
 1 /*简单聪明的办法*/
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int a, b;
 8     cin >> a >> b;
 9     string s = to_string(a + b);
10     int len = s.length();
11     for(int i = 0; i < len; i ++)
12     {
13         cout << s[i];
14         if(s[i] == '-') continue;
15         if((i + 1) % 3 == len % 3 && i != len - 1)
16             cout << ",";
17     }
18     return 0;
19 }

 

标签:case,PAT,cout,Format,int,sum,include,1001
From: https://www.cnblogs.com/marswithme/p/17133293.html

相关文章

  • PAT-basic-1020 月饼 java
    一、题目月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大......
  • PAT-basic-1016 部分A+B java
    一、题目正整数 A 的“DA​(为1位整数)部分”定义为由 A 中所有 DA​ 组成的新整数 PA​。例如:给定 A=3862767,DA​=6,则 A 的“6部分”PA​ 是66,因为 A 中......
  • PAT-basic-1018 锤子剪刀布 java
    一、题目大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大......
  • PAT-basic-1015 德才论 java c++
    一、题目宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而......
  • Geospatial,Hyperloglog,BitMap
    geospatial:地理位置底层实现geoadd:添加 geopos:获取指定的经纬度 geodist georadius:获取指定位置的指定半径的数据 georadiusbymember:获取指定元素指......
  • PAT-basic-1014 福尔摩斯的约会 java
    一、题目大侦探福尔摩斯接到一张奇怪的字条:我们约会吧!3485djDkxh4hhGE2984akDfkkkkggEdsbs&hgsfdkd&Hyscvnm大侦探很快就明白了,字条上奇怪的乱码实际上就是约......
  • PAT-basic-1012 数字分类 java
    一、题目给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:A1​ =能被5整除的数字中所有偶数的和;A2​ =将被5除后余1的数字按给出顺序进行交......
  • PAT-basic-1011 A+B 和 C java
    一、题目给定区间[−231,231]内的3个整数 A、B 和 C,请判断 A+B 是否大于 C。输入格式:输入第1行给出正整数 T (≤10),是测试用例的个数。随后给出 T 组......
  • PAT-basic-1010 一元多项式求导 java
    一、题目设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为nxn−1。)输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以......
  • PAT-basic-1008 数组元素循环右移问题 java
    一、题目一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由(A0A1⋯AN−1)变换为(AN−M⋯AN−1A0A1⋯AN−M−1)(最后M个数......