现在的位置: 首页 > 综合 > 正文

pku1013称硬币

2014年02月12日 ⁄ 综合 ⁄ 共 2438字 ⁄ 字号 评论关闭

先来了解一下 strchr( ) 函数:

原型:extern char *strchr(const char *s,char c);

头文件:#include <string.h>

功能:查找字符串s中首次出现字符c的位置

说明:返回首次出现c的位置的指针,如果s中不存在c则返回NULL。

举例1:(在Visual C++ 6.0中运行通过)

 

#include <string.h>
#include <stdio.h>
int main(void)
{
	char string[15];
	char *ptr, c = 'r';
	strcpy(string, "This is a string");
	ptr = strchr(string, c);
	if (ptr)printf("The character %c is at position: %d/n", c, ptr-string);
	else printf("The character was not found/n");
	return 0;
}

举例2:

#include <stdio.h>
#include <string.h>
int main()
{
	char temp[32];
	memset(temp,0,sizeof(temp));
	strcpy(temp,"Golden Global View");
	char *s = temp;
	char *p,c='v';
	p=strchr(s,c);
	if(p) printf("%s",p);
	else printf("Not Found!");
	return 0;
}

poj(pku)1013代码

#include<stdio.h>
#include<string.h>
char left[3][7],right[3][7],result[3][5];
//设置两个函数分别判断假币是轻还是重于真币
bool dealLight(char x)   
{   
	int i;    
	for(i=0;i<3;i++)       
	{           
		switch(result[i][0])           
		{        
		case 'u':               
			if(strchr(right[i],x)==NULL) return false;   //假币在右边,所以右边应该能找到该字符            
			break;           
		case 'd':               
			if(strchr(left[i],x)==NULL)  return false;   //假币在左边,所以左边应该能找到该字符            
			break;           
		case 'e':               
			if(strchr(right[i],x)!=NULL  ||  strchr(left[i],x)!=NULL)  return false;   //两边全都是真币,所以两边都找不到该字符            
			break;           
		}       
	}       
	return true;   
}
//同理,在下面的函数中,strchr也是这么用的
bool dealHeavy(char x)   
{   
	int i;    
	for(i=0;i<3;i++)       
	{           
		switch(result[i][0])           
		{        
		case 'u':               
			if(strchr(left[i],x)==NULL) 
				return false;
			break;          
		case 'd':               
			if(strchr(right[i],x)==NULL)  
				return false;              
			break;           
		case 'e':               
			if(strchr(right[i],x)!=NULL  ||  strchr(left[i],x)!=NULL)  
				return false;               
			break;           
		}       
	}       
	return true;   
}   
int main()
{
	int t,i;
	char c;
	scanf("%d",&t);
	while(t--){
		for(i=0;i<3;i++)
			scanf("%s%s%s",left[i],right[i],result[i]);
		for(c='A';c<='L';c++){
			if(dealLight(c)==1){
				printf("%c is the counterfeit coin and it is light./n",c);break;
			}
			if(dealHeavy(c)==1){
				printf("%c is the counterfeit coin and it is heavy./n",c);break;
			}
		}
	}
	return 0;
}

附上几组数据,希望对你A过这道题有帮助

sample input
12
ABCD EFGH even
ABCI EFJK up
ABIJ EFGH even
AGHL BDEC even
JKI ADE up
J K even
ABCDEF GHIJKL up
ABC DEF even
I J down
ABCDEF GHIJKL up
ABHLEF GDIJKC down
CD HA even
A B up
B A down
A C even
A B up
B C even
DEFG HIJL even
ABC DEJ down
ACH IEF down
AHK IDJ down
ABCD EFGH even
AB IJ even
A L down
EFA BGH down
EFC GHD even
BA EF down
A B up
A C up
L K even
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI down
ACEGIK BDFHJL up
ACEGIL BDFHJK down
ACEGLK BDFHJI up

sample output
K is the counterfeit coin and it is light.
I is the counterfeit coin and it is heavy.
I is the counterfeit coin and it is light.
L is the counterfeit coin and it is light.
B is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
L is the counterfeit coin and it is heavy.
A is the counterfeit coin and it is light.
A is the counterfeit coin and it is heavy.
L is the counterfeit coin and it is light.
K is the counterfeit coin and it is heavy

【上篇】
【下篇】

抱歉!评论已关闭.