5. 矿石和身份:有A、B、C三名地质勘探队员对一块矿石进行判断,每人判断两次:

booming 2005-10-14 03:07:18
5. 矿石和身份:有A、B、C三名地质勘探队员对一块矿石进行判断,每人判断两次:
• A两次的判断为:它不是铁矿石,不是铜矿石。
• B两次的判断为:它不是铁矿石,是锡矿石。
• C两次的判断为:它不是锡矿石,是铁矿石。
在这三名队员中,有工程师、技术员和实习生各一名,并且:
• 工程师的两次判断都正确;
• 技术员的两次判断中,只有一次是正确的;
• 实习生的两次判断都不正确。
请用C语言程序裁定该矿石是什么矿以及这三人的身份各是什么。
...全文
902 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
aheadyes 2005-10-15
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
enum Kind{isIron=0,isCopper,isTin};
struct asserts{
int a[3]; //a[0]:铁,a[1]:铜,a[2]: 锡;
int rc;//回答对问题的个数
}c[3] = {{0,0,-1,0},{0,-1,1,0},{1,-1,0,0}};

int r[3]={0,0,0};//r[i]==1表示回答对i个问题
int right(int k)
{
int i=0,j=0;
for(i=0; i<3; i++)
{

for(j=0; j<3; j++)
{
if(k==j)
{
if(c[i].a[k]==1)
c[i].rc++;
}
else if((c[i].a[j]!=-1)&& !c[i].a[j])
c[i].rc++;
}

r[c[i].rc] = 1;
}

for(i=0; i<3; i++)
if(r[i] == 0)
return 0;
return 1;
}

void reset()
{
int i;
for(i=0; i<3;i++)
{
r[i] = 0;
c[i].rc = 0;
}
}
void check_p()
{
int i;
for(i=0; i<3; i++)
{
if(c[i].rc==0)
printf(" %c是实习员 ",i+65);
else if(c[i].rc==1)
printf(" %c是技术员 ",i+65);
else
printf(" %c是工程师 ",i+65);
}
}
void check()
{

if(right(isCopper))
{
printf("铜矿石");
check_p();
return;
}
reset();

if(right(isIron))
{
printf("铁矿石");
check_p();
return;
}
reset();

if(right(isTin))
{
printf("锡矿石\n");
check_p();
return;
}
}

int main()
{
check();
system("pause");
return 0;
}
conquer2004 2005-10-14
  • 打赏
  • 举报
回复
上面有个比较不好的地方是:getJob(int m),getMineName(int m)函数,最好是定义一个数组,然后把其中的参数m作为数组的索引得到值。好像C中不直接支持String。
上面运行过了,得到结果如下:

矿石是:铁
A是技术员
B是学生
C是专家
conquer2004 2005-10-14
  • 打赏
  • 举报
回复
用Java做了一个:
class Miner
{
public static int tong = 1;
public static int tie = 2;
public static int xi = 3;
public static int mine = -1;
public static int checkA(int m)
{
int counter = 0;
if(m!=tie)
{
counter++;
}
if(m!=xi)
{
counter++;
}
return counter;
}
public static int checkB(int m)
{
int counter = 0;
if(m!=tie)
{
counter++;
}
if(m==xi)
{
counter++;
}
return counter;
}
public static int checkC(int m)
{
int counter = 0;
if(m == tie)
{
counter++;
}
if(m!=xi)
{
counter++;
}
return counter;
}

public static void sort(int []a)
{
int temp = -1;
if(a[0]>a[1])
{
temp=a[0];
a[0]=a[1];
a[1]=temp;
}
if(a[1]>a[2])
{
temp=a[1];
a[1]=a[2];
a[2]=temp;
}
if(a[0]>a[1])
{
temp=a[0];
a[0]=a[1];
a[1]=temp;
}
}
public static boolean OK(int [] result)
{
for(mine = tong;mine <= xi;mine++)
{
result[0] = checkA(mine);
result[1] = checkB(mine);
result[2] = checkC(mine);
sort(result);
if(result[0] == 0 && result[1] == 1 &&result[2] == 2)
{
return true;
}
}
return false;

}
public static String getMineName(int m)
{

switch(m)
{
case 1:
return "铜";
case 2:
return "铁";
case 3:
return "锡";
}
return null;

}
public static String getJob(int m)
{
switch(m)
{
case 0:
return "学生";
case 1:
return "技术员";
case 2:
return "专家";
}
return null;
}
public static void main(String [] args)
{
int [] a = new int[3];
if(OK(a) && mine<=3)
{

System.out.println("矿石是:"+getMineName(mine));
System.out.println("A是"+getJob(Miner.checkA(mine)));
System.out.println("B是"+getJob(Miner.checkB(mine)));
System.out.println("C是"+getJob(Miner.checkC(mine)));
}
else
{
System.out.println("没有答案");
}

}
}
xiaoshu32 2005-10-14
  • 打赏
  • 举报
回复
矿石为铁矿石
A为技术员
B为实习生
C为工程师
booming 2005-10-14
  • 打赏
  • 举报
回复
•是乱码。

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧