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). ( 计算a+b的和并且按照标准要求输出————当|a+b|>999时,输出必须三位一组并且用逗号分开)。

    输出示例:

    算法流程:

    代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <cmath>
    int main()
    {
        int a,b;
        int len=0,term,res[7];
        scanf("%d%d",&a,&b);
        term=a+b;
        if(term<0)
        {
            printf("-");
        }
        term = abs(term);
        do{
            res[len] = term%10;
            term/=10;
            len++;
        }
        while(term!=0);
        len-=1;
        if(len<3)
        {
            for(int i=len;i>=0;i--)
            {
                printf("%d",res[i]);
            }
        }
        else
        {
            for(int i=len;i>=0;i--)
            {
                if((i+1)%3==0 && i!=len)
                {
                    printf(",");
                }
                printf("%d",res[i]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    idea 使用
    scala
    Java开发工具
    ActiveMQ基础
    Java 多线程实战
    Java 内部类和Lambda
    Spring 学习
    平滑重启php
    opcache
    redis的hscan命令
  • 原文地址:https://www.cnblogs.com/kadcyh/p/14332533.html
Copyright © 2011-2022 走看看