Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

终于过了,999+9是个好测试用例

Posted by jasonnk at 2008-06-27 07:04:33 on Problem 1503
#include <iostream>
#include <string>
using namespace std;

const int MAX = 103;

string a,b,result;

void ltrim(string& str)
{
   string::size_type pos1 = str.find_first_not_of('0');
   str = str.substr(pos1 == string::npos ? 0 : pos1, str.length() - 1);
}

void expand(string& str)
{
	int i=str.length();
	while(i++<MAX){
		str = "0" + str;
	}
}

int main(){
	int isover = 0;
	cin >> result;
	while(cin >> a){
		if(a == "0") break;
		int l1,l2;
		l1 = a.length();
		l2 = result.length();
		expand(result);
		expand(a);
		int l = (l1>l2)?l1:l2;
		int r=0;
		for(int i=0;i<l||isover;i++){
			r = (a[MAX-i-1]-'0')+ (result[MAX-i-1]-'0') + isover;
			int rest = r%10;
			result[MAX-i-1] = rest + '0';
			isover = r/10;
		}
	}
	
	ltrim(result);
	cout << result << endl;
	
	return 1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator