Poj OpenJudge 百练 2602 Superlong sums

1.Link:

http://poj.org/problem?id=2602

http://bailian.openjudge.cn/practice/2602/

2.Content:

Superlong sums

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22337   Accepted: 6577

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.

Source

Ural State University collegiate programming contest 2000

3.Method:

大数加法,直接套模板

http://www.cnblogs.com/mobileliker/p/3512632.html

4.Code:

#include <string>
#include <cstdio>
#include <iostream>

using namespace std;

string sum(string s1,string s2)
{
    if(s1.length()<s2.length())
    {
        string temp=s1;
        s1=s2;
        s2=temp;
    }
    int i,j;
    for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
    {
        s1[i]=char(s1[i]+(j>=0?s2[j]-‘0‘:0));   //注意细节
        if(s1[i]-‘0‘>=10)
        {
            s1[i]=char((s1[i]-‘0‘)%10+‘0‘);
            if(i) s1[i-1]++;
            else s1=‘1‘+s1;
        }
    }
    return s1;
}

int main()
{
    //freopen("D://input.txt","r",stdin);

    int i;

    int n;
    scanf("%d\n",&n);

    string str1(n,‘\0‘);
    string str2(n,‘\0‘);

    for(i = 0; i < n; ++i) scanf("%c %c\n",&str1[i],&str2[i]);

    //for(i = 0; i < n; ++i) printf("%c",str1[i]);
    //printf("\n");
    //for(i = 0; i < n; ++i) printf("%c",str2[i]);
    //printf("\n");

    string res = sum(str1,str2);

    if(res.size() < n)
    {
        i = n - res.size();
        while(i--) cout << "0";
    }
    cout << res << endl;

    return 0;
}

5.Reference:

时间: 2024-08-05 11:17:04

Poj OpenJudge 百练 2602 Superlong sums的相关文章

Poj OpenJudge 百练 1860 Currency Exchang

1.Link: http://poj.org/problem?id=1860 http://bailian.openjudge.cn/practice/1860 2.Content: Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20706   Accepted: 7428 Description Several currency exchange points are working

Poj OpenJudge 百练 2389 Bull Math

1.Link: http://poj.org/problem?id=2389 http://bailian.openjudge.cn/practice/2389/ 2.Content: Bull Math Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13067   Accepted: 6736 Description Bulls are so much better at math than the cows. The

Poj OpenJudge 百练 1062 昂贵的聘礼

1.Link: http://poj.org/problem?id=1062 http://bailian.openjudge.cn/practice/1062/ 2.Content: 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37631   Accepted: 10872 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金

Poj OpenJudge 百练 1573 Robot Motion

1.Link: http://poj.org/problem?id=1573 http://bailian.openjudge.cn/practice/1573/ 2.Content: Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10856   Accepted: 5260 Description A robot has been programmed to follow the instru

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

Poj OpenJudge 百练 Bailian 1008 Maya Calendar

1.Link: http://poj.org/problem?id=1008 http://bailian.openjudge.cn/practice/1008/ 2.content: Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66971   Accepted: 20644 Description During his last sabbatical, professor M. A. Ya

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

poj 大数 - 2602 Superlong sums

这应该算是最简单的大数题了...目的就是为了让你知道char的输入输出比int快很多,还学会了getchar(),putchar(). #include<stdio.h> #define MAX 1000002 char num[MAX],num1[MAX]; int main(){ int n,i; scanf("%d",&n); getchar(); for(i=1;i<=n;i++){ num[i] = getchar(); getchar(); num

Openjudge 百练 / 2524 - 宗教信仰 [并查集]

URL: http://bailian.openjudge.cn/practice/2524/ [描述] 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 50000),你不太可能询问每个人的宗教信仰,因为他们不太愿意透露.但是当你同时找到2名学生,他们却愿意告诉你他们是否信仰同一宗教,你可以通过很多这样的询问估算学校里的宗教数目的上限.你可以认为每名学生只会信仰最多一种宗教. [输入] 输入包括多组数据. 每组数据的第一行包括n和m,0 &l