pku 1013 解题报告

来源:互联网 发布:查航班软件 编辑:程序博客网 时间:2024/06/05 19:23

题意:给出了三次天平的称重且一定能够从这三次称重中找出那个假的硬币,并且要求输出是比真的硬币轻了还是重了。

解题方法:由于最多只有13个银币所以采用了枚举。

代码(学习来源:《程序设计在线导引》):

//strchr()函数是只是否那个字符在字符串中出现,返回的值为出现的位置。可以百度百科去查看下

#include <stdio.h>
#include <string.h>
char l[3][7],r[3][7],p[3][5];
int light(char c)//比真银币轻的检测
{
 int i;
 for(i=0;i<3;i++)
 {
  switch(p[i][0])
  {
  case 'u':
   if(strchr(r[i],c)==NULL)
    return 0;
   break;
  case 'e':
   if(strchr(l[i],c)!=NULL || strchr(r[i],c)!=NULL)
    return 0;
   break;
  case 'd':
   if(strchr(l[i],c)==NULL)
    return 0;
   break;
  }
 }
 return 1;
}
int heavy(char c))//比真银币重的检测

{
 int i;
 for(i=0;i<3;i++)
 {
  switch(p[i][0])
  {
  case 'u':
   if(strchr(l[i],c)==NULL)
    return 0;
   break;
  case 'e':
   if(strchr(l[i],c)!=NULL || strchr(r[i],c)!=NULL)
    return 0;
   break;
  case 'd':
   if(strchr(r[i],c)==NULL)
    return 0;
   break;
  }
 }
 return 1;
}
int main()
{
 int x,i;

 char c;
 scanf("%d",&x);
 while(x--)
 {
  for(i=0;i<3;i++)
   scanf("%s%s%s",l[i],r[i],p[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;
}

AC TIME:0MS

原创粉丝点击