PAT-甲级-1001-A+B Format

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 −. 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存为字符数组,然后每三位分隔。



 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define N 10
 5 
 6 
 7 int main(){
 8   int a,b,c;
 9   char s[N];
10   scanf("%d%d",&a,&b);
11   c=a+b;
12   if(c<0){
13     c=-c;
14     printf("-");
15   }
16   sprintf(s,"%d",c);
17   for(int i=0; s[i]!='\0'; i++){
18     printf("%c",s[i]);
19     if ((strlen(s)-i-1)%3 == 0 && i!=(strlen(s)-1))
20       printf(",");
21   }
22   return 0;
23 }

 

 



posted @ 2018-10-11 23:57  T丶jl  阅读(351)  评论(0编辑  收藏  举报