1001. A+B Format (20) ——PAT (Advanced Level) Practise

题目信息:

1001. A+B Format (20)

时间限制

400 ms

内存限制

32000 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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 <stdio.h>
int main()
{
	int a, b;
	char arr[30], flag;
	arr[29] = '\0';
	while (scanf("%d%d", &a, &b) != EOF)
	{
		int k = 29;
		int s = a + b, t = 0;
		flag = 0;
		if (s < 0)
		{
			flag = 1;
			s *= -1;
		}
		if (s == 0)
			arr[--k] = '0';
		while (s)
		{
			arr[--k] =  '0' + s % 10;
			if (++t == 3)
			{
				t = 0;
				if (s >= 10)
					arr[--k] = ',';
			}
			s /= 10;
		}
		if (flag)
			arr[--k] = '-';
		printf("%s\n", arr + k);
	}
	return 0;
}
时间: 2024-10-10 22:02:48

1001. A+B Format (20) ——PAT (Advanced Level) Practise的相关文章

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73

1008. Elevator (20)——PAT (Advanced Level) Practise

题目信息: 1008. Elevator (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator w

1005. Spell It Right (20)——PAT (Advanced Level) Practise

题目信息: 1005. Spell It Right (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specificat

1011. World Cup Betting (20)——PAT (Advanced Level) Practise

题目信息: 1011. World Cup Betting (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing

1096. Consecutive Factors (20)——PAT (Advanced Level) Practise

题目信息 1096. Consecutive Factors (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the three consecu

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

1027. Colors in Mars (20)【进制转换】——PAT (Advanced Level) Practise

题目信息 1027. Colors in Mars (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Re

1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen. Now given a string that