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

排除法+代入法, 0ms AC

Posted by CodesW at 2016-05-05 14:56:23 on Problem 1013
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static char *weighStr[] = {"light", "heavy"} ; 

int isCharInStr(char c, char *str, int len)
{
	int i = 0 ;

	for(i = 0; i < len; i++)
	{
		if(str[i] == c)
			return 1 ;
	}

	return 0 ;
}

int unrealWeighGet(char c, char *l, char *r, char *w)
{
	int tmp = 0 ;
	int len = strlen(l) ;

	if(isCharInStr(c, l, len))
		tmp = 0 ;
	else if(isCharInStr(c, r, len))
		tmp = 1 ;
	else
		return -1 ;

	if('u' == w[0])
		return (tmp == 0) ? 1 : 0 ;
	else 
		return (tmp == 0) ? 0 : 1 ;
}

int main(int argc, char *argv[])
{
	int  n = 0 ;
	char l[3][8] ;
	char r[3][8] ;
	char w[3][8] ;
	char unreals[12] = {0} ;
	int  len = 0 ;
	int  weigh = 0 ;
	int  lastWeigh = 0 ;
	int  i = 0, j = 0, k = 0 ;

	scanf("%d", &n) ;
	while(n--)
	{
		for(i = 0; i < 3; i++)
		{
			memset(l[i], 0, sizeof(l[0])) ;
			memset(r[i], 0, sizeof(r[0])) ;
			memset(w[i], 0, sizeof(w[0])) ;
		}
		memset(unreals, 0, sizeof(unreals)) ;

		for(i = 0; i < 3; i++)
		{
			scanf("%s %s %s", l[i], r[i], w[i]) ;
			len = strlen(l[i]) ;

			if('e' == w[i][0])
			{
				for(j = 0; j < len; j++)
				{
					unreals[l[i][j] - 'A'] = 2 ;
					unreals[r[i][j] - 'A'] = 2 ;
				}
			}
			else
			{
				for(j = 0; j < len; j++)
				{
					if(0 == unreals[l[i][j] - 'A'])
						unreals[l[i][j] - 'A'] = 1 ;
					if(0 == unreals[r[i][j] - 'A'])
						unreals[r[i][j] - 'A'] = 1 ;
				}
				for(j = 0; j < 12; j++)
				{
					if(0 == unreals[j])
						unreals[j] = 2 ;
				}
			}
		}


		for(k = 0; k < 12; k++)
		{
			if(unreals[k] != 1)
				continue ;
		
			lastWeigh = -1 ;
			for(i = 0; i < 3; i++)
			{
				if('e' == w[i][0])
					continue ;

				weigh = unrealWeighGet(k + 'A', l[i], r[i], w[i]) ;
				if(lastWeigh != -1 && lastWeigh != weigh)
				{
					unreals[k] = 2 ;
					continue ;
				}
				lastWeigh = weigh ;
			}

			if(1 == unreals[k])
			{
				printf("%c is the counterfeit coin and it is %s.\r\n", k+'A', weighStr[weigh]) ;
				break ;
			}
		}
	}

	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