1001 A+B Format (20分)
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. 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
代码讲解:我想的是把这个和变成字符串,然后逐个的往里面插',',最后输出字符串
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int i,j=0,a,b,str=0; 6 scanf("%d %d",&a,&b); 7 a=a+b; 8 if(a<0) //让这个和先不考虑政府,一律变成正数 9 b=-a; 10 else 11 b=a; 12 char c[200]; //结果字符串 13 do 14 { 15 c[str++]=b%10+'0'; //把和从低位到高位逐个弄到字符串中 16 j++; 17 if(j%3==0) //当后面已经有三个字符了果断弄进去一个',' 18 { 19 j=0; 20 c[str++]=','; 21 } 22 b/=10; 23 }while(b); //最后是后循环遍历,这样做的好处是,0不会忘。。 24 if(c[str-1]==',')str--; //把有可能第一位就是','给弄掉 25 if(a<0) c[str++]='-'; //把负数的情况加上 26 for(i=str-1;i>=0;i--) //输出结果字符串 27 { 28 printf("%c",c[i]); 29 } 30 printf("\n"); 31 return 0; 32 }
                    
                
                
            
        
浙公网安备 33010602011771号