zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) Practise:1001. A+B Format

    【题目链接】

    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
    

    题意分析:

    输出(a+b)的结果,输出格式需要使用逗号把数值分割。

    提交代码:

     1 #include <stdio.h>
     2 
     3 int main(void)
     4 {
     5     int out[10];
     6     int i, len, tmp;
     7     int a, b, c;
     8 
     9     scanf("%d %d", &a, &b);
    10 
    11     c = a + b;
    12     if(c < 0)
    13     {
    14         printf("-");
    15         c = -c;
    16     }
    17 
    18     len = 0;
    19     do {
    20         out[len] = c % 10;
    21         c /= 10;
    22         len++;
    23     } while(c != 0);
    24 
    25     for(i = 0; i < len; i++)
    26     {
    27         printf("%d", out[len-1-i]);
    28         tmp = len - i - 1;
    29         //if((len-i-1)%3 == 0 && (len-i) > 1)
    30         if(tmp != 0 && tmp % 3 == 0)
    31             printf(",");
    32     }
    33 
    34     return 0;
    35 }
  • 相关阅读:
    mysql命令行如何得到表结构
    liunx cron问题
    关于SQL判断某个值的查询
    给自己的nexus私人仓库添加缺少的jar包
    MyEclipse9 Maven开发Web工程 详细配置
    springMVC 之 Controller
    java 动态代理
    Freemarker
    java编程陷阱
    HttpClient应用
  • 原文地址:https://www.cnblogs.com/utank/p/4775653.html
Copyright © 2011-2022 走看看