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<cstdio>
    int num[10];
    int main(){
        int a,b;
        scanf("%d%d",&a,&b);
        int sum = a + b;
        if(sum < 0){
            printf("-");
            sum = -sum;
        }
        int len = 0; 
        if(sum == 0) num[len++] = 0; //可以用do while()循环就不用对len=0进行特殊处理 
        while(sum){
            num[len++] = sum % 10;
            sum /= 10;
        }
        for(int i = len - 1; i >= 0; i--){
            printf("%d",num[i]);
            if(i > 0 && i % 3 == 0) printf(",");
        }
        return 0;
    }
    //printf输出格式中,%3d输出三位整数,位数不足高位补空格 %03位数不足补0 
    #include<cstdio>
    int num[10];
    int main(){
        int a,b;
        scanf("%d%d",&a,&b);
        int sum = a + b;
        if(sum < 0){
            printf("-");
            sum = -sum;
        }
       if(sum >= 1000000)  printf("%d,%03d,%03d",sum/1000000,sum%1000000/1000,sum%1000); //第二个数要mod1000000 且等号不能少
       else if(sum > 1000) printf("%d,%03d",sum/1000,sum%1000); //1000通过 
       else {
           printf("%d",sum);
       }
        return 0;
    }

     2019.7.7

    #include<stdio.h>
    
    int main()
    {
        int a,b;
        int arr[10];
        int sum;
        scanf("%d%d",&a,&b);
        sum = a + b;
        int len = 0;
        if(sum < 0)
        {
            printf("-");
            sum = -sum;
        }
        if(sum == 0)
        {
            printf("0
    ");
        }
        while(sum)
        {
            arr[len++] = sum % 10;
             sum /= 10;
        }
        for(int i = len - 1; i >= 0; i--)
        {
            printf("%d",arr[i]);
            if(i % 3 == 0 && i != 0)
            {
                printf(",");
            }
        }
        return 0;
    }
  • 相关阅读:
    CAS 认证
    最近邻规则分类(k-Nearest Neighbor )机器学习算法python实现
    scikit-learn决策树的python实现以及作图
    module object has no attribute dumps的解决方法
    最新Flume1.7 自定义 MongodbSink 结合TAILDIR Sources的使用
    数据探索中的贡献度分析
    python logging模块按天滚动简单程序
    Flume性能测试报告(翻译Flume官方wiki报告)
    python apsheduler cron 参数解析
    python pyspark入门篇
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/9384611.html
Copyright © 2011-2022 走看看