PAT (Advanced Level) Practise: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
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 9Sample Output
-999,991題意分析:
輸出(a+b)的結果,輸出格式需要使用逗號把數值分割。
提交代碼:
1 #include <stdio.h> 2 3 int main(void) 4 { 5 int out[10]; 6 int i, len, tmp; 7 int a, b, c; 8 9 scanf("%d %d", &a, &b); 10 11 c = a + b; 12 if(c < 0) 13 { 14 printf("-"); 15 c = -c; 16 } 17 18 len = 0; 19 do { 20 out[len] = c % 10; 21 c /= 10; 22 len++; 23 } while(c != 0); 24 25 for(i = 0; i < len; i++) 26 { 27 printf("%d", out[len-1-i]); 28 tmp = len - i - 1; 29 //if((len-i-1)%3 == 0 && (len-i) > 1) 30 if(tmp != 0 && tmp % 3 == 0) 31 printf(","); 32 } 33 34 return 0; 35 }?
轉載于:https://www.cnblogs.com/utank/p/4775653.html
總結
以上是生活随笔為你收集整理的PAT (Advanced Level) Practise:1001. A+B Format的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Atitti.java exp ast
- 下一篇: 获取iOS系统版本