首页 > 代码库 > Poj OpenJudge 百练 2602 Superlong sums
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
40 44 26 83 7Sample Output
4750Hint
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:
Poj OpenJudge 百练 2602 Superlong sums
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。