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

贴一下我的代码,轻拍了^_^

Posted by ericlee211 at 2007-01-17 15:57:37 on Problem 1504
#include <iostream>
#include <string>
using namespace std;


string strplus(string left, string right)
{
	
	int large = left.size()>right.size() ? left.size() : right.size();
	int small = left.size()<right.size() ? left.size() : right.size();
	int flag = 0;
	if(left.size()>right.size())
		flag=1;
	string ret=""; int carry=0;
	for(int i=0; i<small; i++)
	{
		int  l=left[i]-'0';
		int  r=right[i]-'0';
		int sum = l+r+carry;
		if(sum>=10)
			carry=1;
		else carry = 0;
		int reted = sum % 10;
		char c = '0'+reted;
		ret += c;
	}
	for(i=small; i<large; i++)
	{
		if(flag)
		{
			int l= left[i]-'0';
			int sum = l+carry;
			if(sum>=10)
				carry = 1;
			else
				carry = 0;
			char c = '0' + sum%10;
			ret += c;
		}
		else
		{
			int l= right[i]-'0';
			int sum = l+carry;
			if(sum>=10)
				carry = 1;
			else
				carry = 0;
			char c = '0' + sum%10;
			ret += c;
		}
	}
	if(carry == 1)
	{
		char c = '1';
		ret += c;
	}
	int len=0;
	for(i=0; i<ret.size();i++)
	{
		if(ret[i]=='0')
		{
			len++;
		}
		else
		{
			break;
		}
	}
	ret.erase(0,len);

	return ret;
}
int main()
{
	int cases;
	cin>>cases;
	string left, right;
	while(cases--)
	{
		cin>>left>>right;	
		string ret = strplus(left,right);
		cout<<ret<<endl; 
	}
	return 0;
}

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