1001. A+B Format (20)

1001. A+B Format (20)

Question
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


今天重新做一遍,有了新的想法,使用了string中的insert函数对‘,’进行插入。这个想法特别的直观,完全是按照人的思维来的,不涉及太多代数问题(我的代数不太好……)
主要的算法就是在插入位置的计算上。
首先,计算一共需要多少个‘,’:count = (len - 1)/3
其次计算第一个逗号的位置:first = len - count * 3

  • 如果first == 0, 说明长度len刚好是3的倍数,其第一个位置不该有‘,’,跳到第二个位置 +3即可。

在first位置插入后,插入第二个位置、第三个位置……每次都是+4即可。

代码如下:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(int argc, const char * argv[]) {
    int a, b;
    scanf("%d %d", &a, &b);
    int sum = a + b;
    bool neg = false;

    //在最后输出的时候再添加‘-’
    if (sum < 0) {
        sum = -sum;
        neg = true;
    }

    //把int转换成string
    stringstream stream;
    stream << sum;
    string result;
    stream >> result;

    //获取计算所需数据
    int len = result.length();
    int count = (len - 1) / 3;
    int first = len - count * 3;
    int newLen = len + count;

    //在正确的位置插入','
    int i = first;
    while (i < newLen) {
        if (first == 0) {
            i = 3;
        }
        result.insert(i, ",");
        i = i + 4;
    }

    if (neg) {
        cout << "-";
    }
    cout << result;

    return 0;
}

之前的想法(不使用string的函数)

int main(int argc, const char * argv[]) {
    int a, b;
    while (scanf("%d%d", &a, &b) != EOF) {
        int sum = a + b;

        if (sum < 0) {
            printf("-");
            sum = -sum;
        }

        int i = 0, divide = 1000, preDivide = 1;
        int result[10];

        while (sum / divide != 0) {
            result[i++] = sum % divide / preDivide;
            divide = divide * 1000;
            preDivide = preDivide * 1000;
        }
        result[i++] = sum/(divide/1000);

        bool isFirst = true;
        do {
            if (isFirst) {
                printf("%d", result[--i]);
            } else {
                printf("%03d", result[--i]);
            }
            if (i != 0) {
                printf(",");
            }
            isFirst = false;
        } while (i > 0);
        printf("\n"); 
    }
}

别人的解法

int main(int argc, const char * argv[]) {
    int a, b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        int ans = a+b;
        int flag = 1;
        if(ans < 0) {
            flag = 0;
            ans = -ans;
        }

        std::string output;
        int cnt = 0;
        do
        {
            int temp = ans%10;
            if(cnt > 0 && cnt%3 == 0)
                output.insert(output.begin(), ',');
            output.insert(output.begin(), temp+'0');
            ans/=10;
            cnt++;
        }while(ans != 0);

        if(flag == 0) output.insert(output.begin(), '-');

        printf("%s\n",output.c_str());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值