POJ 2602
发布日期:2021-06-30 15:30:55 浏览次数:2 分类:技术文章

本文共 1805 字,大约阅读时间需要 6 分钟。

Superlong sums

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23836   Accepted: 7063

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

40 44 26 83 7

Sample Output

4750

Hint

Huge input,scanf is recommended.

Source

解题思路:

大数相加   

借鉴别人说的注意事项:

如果用char[]保存加数和被加数,要用getchar()输入,

如果用int[]保存加数和被加数,要用scanf)输入。

用cin会超时,cin是重载函数,没有指定格式,输入时比较浪费时间

100W的空间不能局部静态申请,单可以全局静态申请,也可以局部动态申请(用new)

最恶心得是,我把结果开头的0(如果有的话)删去,竟然WA,真没见过这样的加法!

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

这是输出要求的格式,竟然要求 被加数、加数、和 的位数一致!!

按这样理解,这题是不允许最高位出现进位的!!

见过奇怪的,没见过这样奇怪的要求 

= =这道题的就是让大家知道字符串输入输出比数组快

其中putchar(),getchar()详解见

代码:

#include
#include
using namespace std;#define MAX 1000002char num[MAX],num1[MAX]; int main(){ int n,i; cin>>n; getchar(); for(i=1;i<=n;i++){ num[i] = getchar(); getchar(); num1[i] = getchar(); getchar(); } for(i=n;i>=1;i--){ num[i] += num1[i]-'0'; if(num[i]>'9'){ num[i]-=10; num[i-1]+=1; } } for(i=1;i<=n;i++){ putchar(num[i]); } cout<

 

转载地址:https://joycez.blog.csdn.net/article/details/82822316 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 3982
下一篇:POJ 2389

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月23日 18时44分03秒