• 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 106a,b106. 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

    Submit:

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        
        int a,b,sum;
        char str[20];
        scanf("%d %d",&a,&b);
        sum = a + b;
        sprintf(str,"%d",sum);//字符串地址,第三个参数类型,写入的数据;该函数为清空写入
        int len = strlen(str);
        for (int i=0;i<len;i++) {
            printf("%c",str[i]);
            if (str[i] == '-') continue;//第一个字符是负号,则执行下一次循环
            if ((i+1)%3 == len%3 && i != len-1) printf(",");//12345678 8位 需要输出为 12,345,678 即 i=1时,2%3==2,8%3==2需要输出‘,’
        }
        return 0;
    }

    参考:

    柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

    昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

  • 相关阅读:
    P2082 区间覆盖(加强版)
    Java基础
    @import
    POST方式"Content-type"是"application/x-www-form-urlencoded 的请求遇到的问题
    VBox 安装 macOS 10.12
    对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
    window.location 属性
    post get 区别
    vue 生命周期
    记事本
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15097677.html
Copyright © 2020-2023  润新知