# PAT Advanced 1001. A+B Format (20) (C语言实现)

## 题目

Calculate 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 Specification:

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

### Output Specification:

For each test case, you should output the sum of and in one line. The
sum must be written in the standard format.

### Sample Input:

``````-1000000 9
``````

### Sample Output:

``````-999,991
``````

## 思路

2018/1/5更新0：重要参考链接
Stack overflow有一个帖子专门讨论如何做这个事情，我自己想到的三个也都是问题里几个高票答案的方法，大家可以学习一下。

• 两数和为0时要输出0;
• 注意逗号的输出位置，如不要在数字前面和后面有输出

2018/1/5更新1：

2018/1/5更新2：

``````#include <stdio.h>
#include <locale.h>

int main(void)
{
setlocale(LC_NUMERIC, "");
printf("%'d\n", 1123456789);
return 0;
}
``````

## 代码

``````#include <stdio.h>
#include <string.h>

int main()
{
int a, b, pos;
char num[11];

scanf("%d%d", &a, &b);
sprintf(num, "%d", a + b);

for(pos = strlen(num) - 3; pos > 0 && num[pos - 1] != '-'; pos -= 3)
{
memmove(num + pos + 1, num + pos, strlen(num) - pos + 1);
num[pos] = ',';
}

puts(num);
return 0;
}
``````