此題沒(méi)思路,是向一位自稱(chēng)菜鳥(niǎo)的牛人學(xué)來(lái)的。但是這個(gè)思想很巧妙
要判斷并找出A到L的字母哪個(gè)重量不標(biāo)準(zhǔn),或者輕或者重。從A到L把所有字母掃一遍,每個(gè)字母判斷它出現(xiàn)的位置,如果在even中出現(xiàn),則掃下一個(gè)字母,如果在up(或down)中出現(xiàn),則可通過(guò)在left還是right中出現(xiàn)來(lái)判斷出輕重,因?yàn)橹挥幸粋€(gè)重量不標(biāo)準(zhǔn)的字母,所以可以直接判斷。
這里用到了一個(gè)很好用的字符串函數(shù)strchr(char *a ,char c),a代表所要搜索的字符串?dāng)?shù)組,c代表想要在a中查找到的字母。即在字符串a(chǎn)中查找有沒(méi)有字母c,如果沒(méi)有則返回NULL也就是0。很好用的函數(shù)啊^-^
還有一點(diǎn)很奇怪,這個(gè)頭文件必須用#include<stdio.h>如果用#include<iostream>在Dev-cpp中會(huì)出現(xiàn)“'right' undeclared(first use this function)”可是我明明定義的全局變量right[3][10]??;在VC++中就沒(méi)有錯(cuò)誤,難道編譯器太嚴(yán)格了?不理解……
Source Code
Problem: 1013 |
|
User: wic |
Memory: 180K |
|
Time: 0MS |
Language: C++ |
|
Result: Accepted |
- Source Code
#include<stdio.h>
#include<string.h>
char left[3][10],right[3][10],judge[3][10];
bool light(char c)
{
int i;
for(i=0; i<3; i++)
switch(judge[i][0])
{
case'u':if(strchr(right[i],c)==NULL)return false;break;
case'e':if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)return false; break;
case'd':if(strchr(left[i],c)==NULL)return false; break;
}
return true;
}
bool heavy(char c)
{
int i;
for(i=0; i<3; i++)
switch(judge[i][0])
{
case'd':if(strchr(right[i],c)==NULL)return false;break;
case'e':if(strchr(left[i],c)!=NULL||strchr(right[i],c)!=NULL)return false; break;
case'u':if(strchr(left[i],c)==NULL)return false; break;
}
return true;
}
int main()
{
int n,i;
char c;
scanf("%d",&n);
while(n--){
for(i=0; i<3; i++)
scanf("%s%s%s",left[i],right[i],judge[i]);
for(c='A'; c<='L'; c++){
if(light(c))
{printf("%c is the counterfeit coin and it is light.\n",c); break;}
if(heavy(c))
{printf("%c is the counterfeit coin and it is heavy.\n",c); break; }
}
}
return 0;
}