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 7Sample Output
4750Hint
Huge input,scanf is recommended.Source
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: