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

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<string.h>
     4 using namespace std;
     5 int main(){
     6     int a, b, c, sign = 0, bt;
     7     char prt[30];
     8     scanf("%d%d", &a, &b);
     9     c = a + b;
    10     if(c < 0){
    11         c = -1 * c;
    12         sign =1;
    13     }
    14     int i = 0, tag = 0;
    15     do{
    16         bt = c % 10;
    17         c = c / 10;
    18         prt[i++] = '0' + bt;
    19         tag++;
    20         if(tag != 0 && tag % 3 == 0){
    21             prt[i++] = ',';
    22         }
    23     }while(c != 0);
    24     if(prt[i - 1] != ',')
    25         prt[i] = '';
    26     else
    27         prt[i - 1] = '';
    28     if(sign == 1)
    29         printf("-");
    30     for(int j = strlen(prt) - 1; j >= 0; j--)
    31         printf("%c", prt[j]);
    32     cin >> a;
    33     return 0;
    34 }
    View Code

    注意:在整个数字的最高位前不能有逗号。

  • 相关阅读:
    Java文档注释
    Java程序基本框架
    Java文件手动编译执行步骤
    JDK安装中配置Path无效解决办法
    JDK安装配置
    Java简单介绍运行机制
    python代码注释
    python从hello world开始
    python,pycharm,anaconda之间的区别与联系
    python环境配置
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8442154.html
Copyright © 2011-2022 走看看