poj 2602 Superlong sums

Superlong sums
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23716   Accepted: 7029

Description

The creators of a new programming language D++ have found out that whatever limit for SuperLongInt type they make, sometimes programmers need to operate even larger numbers. A limit of 1000 digits is so small... You have to find the sum of two numbers with maximal size of 1.000.000 digits.

Input

The first line of an input file contains a single number N (1<=N<=1000000) - the length of the integers (in order to make their lengths equal, some leading zeroes can be added). It is followed by these integers written in columns. That is, the next N lines contain two digits each, divided by a space. Each of the two given integers is not less than 1, and the length of their sum does not exceed N.

Output

Output file should contain exactly N digits in a single line representing the sum of these two integers.

Sample Input

4
0 4
4 2
6 8
3 7

Sample Output

4750

Hint

Huge input,scanf is recommended.

这道题第一遍做超时了,看博客说,用getchar接收字符要比输入整数快。要是有前导0 也不用操心,去掉了反而错了,还有输入一定要小心。

#include<stdio.h>
#include<string>
using namespace std;
#define N 1000010
char s1[N],s2[N];

int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		getchar();
		for(int i=0;i<n;i++)
		{
			s1[i]=getchar(),getchar();
			s2[i]=getchar(),getchar();
		}
		int c=0;
		for(int i=n-1;i>=0;i--)
		{
			c=c+s1[i]-'0'+s2[i]-'0';
			s1[i]=c%10+'0';
			c/=10; 
		}
		if(c)
		printf("1");
		s1[n]='\0';
		puts(s1);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36914923/article/details/80176719