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 }


  • 相关阅读:
    透过书本了解HTML5
    Seam性能讨论
    Maven依赖管理
    Tapestry
    为HTML5的未来制定学习计划
    后缀数组
    HDU 1042(大数)
    教你理解复杂的C/C++声明
    编程修养
    平衡二叉树
  • 原文地址:https://www.cnblogs.com/kking/p/2331825.html
Copyright © 2011-2022 走看看