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

我已经把所有数据都测试并通过了,但提交还是WA。哪位高手帮忙看看??

Posted by jackrhc at 2009-02-15 18:22:03 on Problem 2116
#include <iostream>
#include <string>
using namespace std;
void add(string& num1,int index,char data)
{
	if(index<0)
		num1.insert(num1.begin(),data);
	else
	{
		if(data=='1')
		{
			if(num1.at(index)=='1')
			{
				if(index==(num1.size()-1))
				{
					num1[num1.size()-1]='0';
					add(num1,num1.size()-2,'1');
				}
				else
				{
					num1[index]='0';
					
					if(index<num1.size()-2)
						add(num1,index+2,'1');
					else
						add(num1,num1.size()-1,'1');
					add(num1,index-1,'1');
				}
			}
			else
				num1[index]='1';
		}
	}
}
void changeToCanonical(string& num)
{
	if(num.size()<2)
		return;
	bool out=false;
	while(!out)
	{
		out=true;
	for(int i=num.size()-1;i>0;i--)
	{
		if(num.at(i)=='1'&&num.at(i-1)=='1')
		{
			num[i]='0';
			num[i-1]='0';
			if(i>1)
			{
				if(num[i-2]=='0')
					num[i-2]='1';
				else
				{
					add(num,i-2,'1');
					out=false;
					break;
				}
			}
			else
				num.insert(num.begin(),'1');
		}
	}
	}
}
void removeLeadZear(string& num)
{
	if(num.size()>1)
	{
		while(num.at(0)=='0'&&num.size()>1)
			num.erase(0,1);
	}
}
int main()
{
	int space, i, j;
	string num1, num2, result;
	while(cin>>num1>>num2)
	{
		removeLeadZear(num1);
		removeLeadZear(num2);
		changeToCanonical(num1);
		changeToCanonical(num2);
		result=num1;
		for(i=num2.size()-1,j=result.size()-1;i>=0;i--,j--)
			add(result,j,num2.at(i));
		changeToCanonical(result);
		space=2+result.size()-num1.size();
		for(i=0;i<space;i++)
			cout<<" ";
		cout<<num1<<endl;
		cout<<"+";
		space=1+result.size()-num2.size();
		for(i=0;i<space;i++)
			cout<<" ";
		cout<<num2<<endl;
		cout<<"  ";
		for(i=0;i<result.size();i++)
			cout<<"-";
		cout<<endl<<"  "<<result<<endl<<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