码迷,mamicode.com
首页 > 其他好文 > 详细

(甲)PAT-1001

时间:2016-05-27 23:27:06      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/ly199553/p/5536195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!