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

Re:为便于理解,给组反例,详见内.

Posted by peterpan at 2005-10-12 14:00:43 on Problem 1013
In Reply To:为便于理解,给组反例,详见内. Posted by:ferret_chao at 2005-10-12 12:28:54
感谢ferret
用暴力做出来了


//------------------------------------------
#include <iostream>
#include <algorithm>
using namespace std;


char		coinleft[3][12];
char		coinright[3][12];
int		coin[12];
int		len[3];
int		leftw,rightw;
int		state[3];//0为平衡 -1为up 1为down	

int main()
{
	int i,j,k,l,t;
	int casenum;
	char input[3][1024];

//	freopen("1013.txt", "r", stdin);


	scanf("%d ",&casenum);

	for(i=0;i<casenum;i++)//每次范例
	{

		//读取输入
		for(l=0;l<3;l++)
		{
			//initialize
			len[l] = 0;
			state[l] = 0;
			gets(input[l]);			

            for(k=0;;)
			{
				len[l] = 0;
				for(;isalpha(input[l][k]);k++)//天平左边
				{
					coinleft[l][len[l]] =  input[l][k];
					len[l]++;
				}
				while(isspace(input[l][k]))
				{
					k++;//跳过空格
				}
				len[l] = 0;
				for(;isalpha(input[l][k]);k++)//天平右边
				{
					coinright[l][len[l]] =  input[l][k];
					len[l]++;
				}
				while(isspace(input[l][k]))
				{
					k++;//跳过空格
				}
				if(input[l][k] == 'e')
					state[l]=0;//even
				else if (input[l][k] == 'u')
						state[l]=-1;//up
					else
						state[l]=1;//down
				break;
			}
		}


		char Weight[3][10]={"light","","heavy"};
		//暴力代入破解


		for(j=0;j<12;j++)//假设第j枚为假币
		{
			for(k=-1;k<=1;k=k+2)//两种情况 该假币轻 为-1 重 为1  其他的为0
			{
				for(t=0;t<12;t++)
					coin[t] = 0;
				coin[j] = k;
				for(l=0;l<3;l++)//三个平衡式各自判断 
				{
					leftw = 0;
					rightw = 0;
					for(t=0;t<len[l];t++)
					{
						leftw  += coin [coinleft[l][t]-'A'];
						rightw += coin [coinright[l][t]-'A'];
					}
					if((rightw -leftw) != state[l] )//有一个等式不符合了 跳出 其他等式不必计算 
													//否则 继续验证 直到三个都满足 
					{	
						break;
					}
				}
				if((rightw -leftw) == state[l-1] && l==3 ) //是三个等式都满足的跳出 还是错误的跳出?
												////能走到这里 说明三个等式都符合
				{
					printf("%c is the counterfeit coin and it is %s.\n",'A'+j,Weight[k+1]);
					goto nexttest;//下一个范例
				}
			}// 轻重循环

		}



nexttest:
		;

	}//end of for(i=0;i<casenum;i++)
	

	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