zoukankan      html  css  js  c++  java
  • 1001. A+B Format

    1001. A+B Format (20)

    时间限制
    400 ms
    内存限制
    32000 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 <iostream>
    2 #include <fstream>
    3 #include <vector>
    4 #include <string>
    5 #include <algorithm>
    6 #include <map>
    7 #include <stack>
    8 #include <cmath>
    9 #include <queue>
    10 #include <set>
    11
    12
    13 using namespace std;
    14
    15
    16
    17 string toString( int value )
    18 {
    19
    20 string pre = "";
    21 string result;
    22
    23
    24 if( value < 0 )
    25 {
    26 value = - value;
    27 pre.push_back('-');
    28 }
    29 //else if( value == 0 )
    30 //{
    31 // return "0";
    32 //}
    33 //else
    34 //{
    35 int vl = value;
    36 int index = 0;
    37 while( vl != 0 )
    38 {
    39 if( index != 0 && index%3 == 0 )
    40 {
    41 result.push_back(',');
    42 }
    43
    44 int digit = vl % 10;
    45
    46 result.push_back('0' + digit);
    47
    48 vl = vl/10;
    49 ++index;
    50 }
    51
    52 reverse( result.begin() , result.end() );
    53
    54
    55 return pre + result;
    56 //}
    57
    58
    59
    60 }
    61
    62
    63
    64
    65 int main()
    66 {
    67
    68
    69
    70
    71 int a , b;
    72
    73 while( cin >> a >> b )
    74 {
    75 if( a+b == 0 )
    76 {
    77 cout << "0" << endl;
    78 }
    79 else
    80 {
    81 cout << toString(a+b) << endl;
    82 }
    83
    84 }
    85
    86
    87 return 0;
    88 }


  • 相关阅读:
    linux下mysql基于mycat做主从复制和读写分离之基础篇
    HttpClient之基本使用
    查阅资料学习的书签(补充中.......)
    java架构《Socket网络编程基础篇》
    Redis常见配置文件详解
    redis学习教程五《管道、分区》
    redis学习教程四《管理、备份、客户端连接》
    js将json数组转成tree对象
    小程序转发事件生命周期
    mpvue开发小程序添加页面
  • 原文地址:https://www.cnblogs.com/kking/p/2331825.html
Copyright © 2011-2022 走看看