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 shhu at 2007-03-26 10:25:26 on Problem 1702
这份代码9的时候输出是  empty  1,9
居然AC了

#include <iostream>
#include <list>
using namespace std;


int eva()
{
	int num,i;
	cin>>num;//num中保存的是天平两边的重量差
	int side=1;//left=-1,right =1;
	//side用来标记天平那一侧更轻
	int ruler[30];//保存每个砝码的重量
	
	int sum[30];//保存砝码重量的求和
	sum[1]=1;
	ruler[1]=1;
	for (i=2;i<=20;i++) ruler[i]=ruler[i-1]*3;
	for (i=2;i<=20;i++) sum[i]=sum[i-1]+ruler[i];
	//初始化砝码重量
	
	list<int> left,right; //保存结果
	for (i=20;i>0;i--)//从重到轻枚举每一个砝码
	{
		if (num>=ruler[i])
			//如果重量差大于当前砝码,则将它加到较轻的一边
		{
			//big poise
			if (side==1) right.push_front(i);
			else left.push_front(i);
			num-=ruler[i];
		}
		else if (num>sum[i-1])
			//如果重量差大于所有轻于当前砝码的砝码的质量和,则将当前砝码加到较轻的一边
		{
			//big poise-r
			if (side==1) right.push_front(i);
			else left.push_front(i);
			num=ruler[i]-num;
			side*=-1;//加砝码后,轻重两边将会对调
		}
	}
	list<int>::iterator  pList;
	char buf[1000];
	//显示结果
	if (left.empty())
	{
		cout<<"empty";
	}
	else
	{
		pList=left.begin();
		sprintf(buf,"%d",ruler[*pList]);
		cout<<buf;
		pList++;
		for (;pList!=left.end();pList++)
		{
			sprintf(buf,",%d",ruler[*pList]);
			cout<<buf;
		}
	}
	cout<<' ';
	if (right.empty())
	{
		cout<<"empty";
	}
	else
	{
		pList=right.begin();
		sprintf(buf,"%d",ruler[*pList]);
		cout<<buf;
		pList++;
		for (;pList!=right.end();pList++)
		{
			sprintf(buf,",%d",ruler[*pList]);
			cout<<buf;
		}
	}
	cout<<endl;
	return 0;
}

int main()
{
	int t;
	cin>>t;
	while(t--) eva();
	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