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

    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


    此题要点在于格式化输出,美式数字表达法

    #include <stdio.h>
    #include<string.h>
    int main()
    {
        int a,b;
        char str[10];
    
        scanf("%d%d", &a, &b);
        a+=b;
        // 对负数处理为正数后以便放入数组处理,记得先输出负号
        if(a<0){
            a = -a;
            printf("-");
        }
        // 格式化转为字符串,存放到str中
        b = sprintf(str, "%d", a);
        for(a=0; a<b; a++){
            printf("%c", str[a]);
    
            // 美式数字输出条件判断
            if(b%3==(a+1)%3&&b!=(a+1)){
                printf(",");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    淘宝放大镜
    碰撞的小球
    模拟微博发布
    CSS兼容IE Firefox问题与解决方法
    太阳八大行星运行轨迹
    Canvas标签基础
    offsetTop、clientTop、scrollTop、offsetTop
    js绘制圆形时钟
    js时钟
    js五星好评2
  • 原文地址:https://www.cnblogs.com/baichangfu/p/7158711.html
Copyright © 2011-2022 走看看