zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    The Balance POJ 2142 扩展欧几里得
    扩展欧几里得定理总结
    Crashing Robots POJ 2632 简单模拟
    POJ 1328 Radar Installation 贪心算法
    The Pilots Brothers' refrigerator DFS+枚举
    HDU RSA 扩展欧几里得
    HDU A/B 扩展欧几里得
    ACM 数学
    E
    BZOJ 3223: Tyvj 1729 文艺平衡树
  • 原文地址:https://www.cnblogs.com/fengyanlover/p/5356613.html
Copyright © 2011-2022 走看看