pku 1013 解题报告

硬币真假识别算法

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

解题方法:由于最多只有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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值