1001. A+B Format

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


题目大意就是计算两个数的和,以特定的规则打印出来——满三位用逗号隔开


#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int a, b, s;
    char t;
    vector<char> v;    // 将结果转换为字符串,用vector容器保存 
    cin >> a >> b;
    s = a + b;
    // 处理两个特殊情况 
    if(s == 0){
        v.push_back('0');
    }
    else if(s < 0){
        cout << '-';
        s = (-1) * s;
    }
     // 从后往前逐个将结果取字符串 ,并添加逗号 
    int c = 0;
    while(s > 0){
        t = s % 10; 
        v.push_back((char)(t + 48));
        s = s / 10;
        c++;
        if(c == 3 && s != 0)     // 这里注意要加 s!=0 这一条件 
        {
            v.push_back(',');
            c = 0;
        }
    }
    // 输出 
    vector<char>::iterator it;
    for(it = v.end() - 1; it >= v.begin(); it--)        
        cout << *it;
    cout << endl;
    
    return 0;
}

 

posted @ 2016-04-05 20:09  Nagihiko  阅读(156)  评论(0编辑  收藏  举报