您的位置:首页 > 其它

poj-2602-Superlong sums

2017-09-29 09:35 447 查看
http://poj.org/problem?id=2602

Superlong sums

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23519 Accepted: 6974
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.

~题意:纵向输入两个数,然后输出相加结果

就是简单高精度加法咯~

#include<iostream>
#include<stdio.h>
using namespace std;
#define maxn 1000005
char a[maxn];
char b[maxn];
char ans[maxn];
int main()
{
int n;
scanf("%d",&n);
getchar();

for(int i=1;i<=n;i++)
{
a[i]=getchar();//字符
getchar();//空格
b[i]=getchar();
getchar();
}
int tmp=0;
for(int i=n;i>=1;i--)
{
tmp=tmp+(a[i]-'0')+(b[i]-'0');
ans[i]=(tmp%10+'0');
tmp/=10;
}
bool flag;//判断最后是否有进位
if(tmp)
{
ans[0]=(tmp+'0');
flag=true;
}
else flag=false;

if(flag)
{
for(int i=0;i<=n;i++)
{
putchar(ans[i]);
}
}
else
{
for(int i=1;i<=n;i++)
{
putchar(ans[i]);
}
}
putchar('\n');

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: