zoukankan      html  css  js  c++  java
  • 1001. A+B Format (20) (%0nd)

    1001. A+B Format (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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

    解题思路:这里主要是注意一下补0的问题

         %nd:n代表的是列宽长度。

         (1)%-nd   -  代表的是左对齐。

         (2)%nd       代表的是右对齐。

         (3)%0nd   0(数字零)代表的是不足n位长度的左补齐0。

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 int main()
     5 {
     6     int a,b;
     7     int sum = 0;
     8     scanf("%d %d",&a,&b);
     9 
    10     sum = a+b;
    11     if( sum<0){
    12         printf("-");
    13         sum = 0-sum;
    14     }
    15     if( sum>=1000000){
    16         printf("%d,%03d,%03d",(sum/1000000),(sum/1000%1000),(sum%1000));
    17     }
    18     else if( sum>=1000){
    19         printf("%d,%03d",(sum/1000),(sum%1000));
    20     }
    21     else printf("%d",sum);
    22     return 0;
    23 }
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    jvm09
    jvm08
    jvm07
    求解最长公共子序列问题
    归并排序
    求解N皇后问题
    快速排序算法
    求解0/1背包问题
    求解全排列问题
    求解最大连续子序列和问题
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8421141.html
Copyright © 2011-2022 走看看