(甲)PAT-1001

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(甲)PAT-1001相关的知识,希望对你有一定的参考价值。

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
 

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

 

主要是题目的groups of three 的of 的用法让我有点迷糊,最后确认是一种属性关系,在题目的意思为3个一组。

代码为:

 1 /*
 2 https://www.patest.cn/contests/pat-a-practise/1001
 3 author: LY   2016.6.26
 4 */
 5 #include<iostream>
 6 #include<string>
 7 #include<algorithm>
 8 using namespace std;
 9 
10 int main()
11 {
12     int a,b,c;
13     string s;
14     scanf("%d%d",&a,&b);
15            
16      c=a+b;
17      int i=0,l=0;
18      int flag=0;   //符号判断
19      if(c<0)
20      {
21          flag=1;
22          c=abs(c);
23      }
24      while((c/10)!=0)
25     {
26         s[i]=(c%10)+0;
27         c=c/10;
28         i++; 
29         l++;
30         if(l%3==0)
31             {
32                 s[i]=,;
33                 i++; 
34             } 
35     } 
36     s[i]= c+0;
37     
38     //对a+b的和进行拆分(每3个数一组) 
39     int count=1;
40     if(flag==1)
41     printf("%c",-); 
42     for(int j=i;j>=0;j--)
43     {
44         printf("%c",s[j]);
45     } 
46     
47     printf("\n"); 
48     return 0;
49 } 

 

以上是关于(甲)PAT-1001的主要内容,如果未能解决你的问题,请参考以下文章

PAT 1001. A+B Format

PAT-1001

pat 1001

PAT1001. A+B Format (20)

PAT 1001 A+B Format

PAT A1001 A+B Format