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

想不出哪儿错了,谁帮忙看看好吗?有注析,c++

Posted by ZHQVC at 2007-07-26 14:29:00 on Problem 1013
#include <stdio.h>
#include <string>

const char con[]={"ABCDEFGHIJKL"};
int flag[12];//用于标志对对应con[]中各个硬币的状态,-1表示没出现,0表示是真币,1表示轻,2表示重
void compare(char s1[],char s2[],char s3[])
{
	int length;
	int i;
	length=strlen(s1);
	if(strcmp(s3,"even")==0)//当两边相等时,把对应的各个硬币的标志设为0
	{
    	for(i=0;i<length;i++)
		{
			 flag[s1[i]-65]=0;
			 flag[s2[i]-65]=0;
		}
	}
	else if(strcmp(s3,"up")==0)//当为up状态时,表时左边s1重,右边s2轻
	{
		for(i=0;i<length;i++)
		{
			if(flag[s1[i]-65]==-1)
			{
				flag[s1[i]-65]=2;//2标记出现在重的一边
			}
			if(flag[s1[i]-65]==1)//出现相反情况时,去除
			{
				flag[s1[i]-65]=0;
			}

			if(flag[s2[i]-65]==-1)
			{
				flag[s2[i]-65]=1;//1标记出现轻的一边
			}
			if(flag[s2[i]-65]==2)//出现相反情况时,去除
			{
				flag[s2[i]-65]=0;
			}
		}
	}
	else//否则为down状态时,表时左边s1轻,右边s2重
	{

		for(i=0;i<length;i++)
		{
			if(flag[s1[i]-65]==-1)//左边标记为轻
			{
				flag[s1[i]-65]=1;
			}
			if(flag[s1[i]-65]==2)//出现相反情况时,去除
			{
				flag[s1[i]-65]=0;
			}

			if(flag[s2[i]-65]==-1)//右边标记为轻
			{
				flag[s2[i]-65]=2;
			}
			if(flag[s2[i]-65]==1)//出现相反情况时,去除
			{
				flag[s2[i]-65]=0;
			}
		}
	}
}
int main()
{
   int i,m;
   char s1[7],s2[7],s3[5];

	scanf("%d",&m);
	while(m>0)
	{
		m--;
		for(i=0;i<12;i++) flag[i]=-1;//初始化标记状态
		for(i=0;i<3;i++)
		{
			scanf("%s%s%s",&s1,&s2,&s3);
			compare(s1,s2,s3);//开始对比
		}
		for(i=0;i<12;i++)
		{
			if(flag[i]==1)
				  printf("%c is the counterfeit coin and it is light.\n",con[i]);
			if(flag[i]==2)
					printf("%c is the counterfeit coin and it is heavy.\n",con[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