zoukankan      html  css  js  c++  java
  • 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 9
    

    Sample 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 }
  • 相关阅读:
    2019春季第五周作业
    2019春第四周作业(基础题)计算机
    2019第三周作业
    第二周基础作业
    2019春第九周作业
    2019年春第八周作业
    第七周总结
    第六周作业
    2019春第五周作业
    2019年春季学期第四周作业
  • 原文地址:https://www.cnblogs.com/utank/p/4775653.html
Copyright © 2011-2022 走看看