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).

计算a+b并以标准格式输出——也就是说,除非数字少于四位,否则数字必须用逗号分隔成三组。

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.

每个输入文件包含一个测试用例。每个用例包含一对整数ab,其中−106≤ab≤106。数字由空格分隔。

Output Specification:

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.

对于每个测试用例,您应该在一行中输出ab的和。和必须以标准格式书写。

Sample Input:

-1000000 9

Sample Output:

-999,991

Solution

#include <iostream>
using namespace std;
int main() {
    int a, b;
    cin >> a >> b;
    string s = to_string(a + b);
    int len = s.length();
    for (int i = 0; i < len; i++) {
        cout << s[i];
        if (s[i] == '-') continue;
        if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";//无敌
    }
    return 0;
}

这一步很关键

if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";

分为两个条件

  1. (i + 1) % 3 == len % 3
  2. i != len - 1

显然,第一个条件是在定位插入 "," 的位置,而第二个条件是在判断边界。

判断边界很好理解。难点是定位插入点。


我们重新理一下本题思路。

我们的目标是通过遍历字符串找到插入点,我们有两个选择:

  1. 从后往前(难点在判断边界)
  2. 从前往后(难点在判断插入点)

这里我们选择第2种。


从直观理解插入点,即从int sum最低位开始,每三个数字添加一个逗号。

我们以示例为例子

Sample Input:

-1000000 9

Sample Output:

-999,991
Index 0 1 2 3 4 5 6 7
Number - 9 9 9 , 9 9 1

从直观看,我们在最后三个下标为7、6、5的数字前加了一个逗号。

而在下标为3、2、1之前,按三个数字一个逗号的规律,我们本应该添加一个逗号,但前方是符号位,数字遍历已经结束。

但我们似乎发现了某些规律,即逗号将数字分为三个三个一组

我们带入长一些的数字观察一下:

1 000 000

Index 0 1 2 3 4 5 6 7 8
Number 1 , 0 0 0 , 0 0 0

10 000 000

Index 0 1 2 3 4 5 6 7 8 9
Number 1 0 , 0 0 0 , 0 0 0

结合以上三个例子,我们可以发现逗号的副作用是分割了数字,将数字分为三个一组。

其中最接近于下标0的一组有字符串大小%3个数字,而这个数字正好是第一个插入点的下标

以后插入点的下标均是第一个插入点的下标+3的倍数

这使从前往后遍历并获得插入点变得可能。

简单推算一下,即可得到题解的判断条件。

if ((i + 1) % 3 == len % 3 && i != len - 1) cout << ",";

思考

  1. 是否能写出从后往前的代码?