有12個外表一模一樣的小球,其中有一個的質量和其他11個球不一樣(11個球的質量完全相同),而且該球不知道是輕點還是重點?,F(xiàn)在給你一個天平(無砝碼),只有3次測量機會,請你找出該球。球號從a到l(小寫)
#include
#include
#include
#define LETTER_HEAD 97 //a的起始
#define BALL_NUM 12 //球個數(shù)
#define NORMAL_WEIGHT 2 //一般球的重量
#define HIGH_WEIGHT 3 //球超重
#define LOW_WEIGHT 1 //球失重...
#define LOOP 3 //循環(huán)次數(shù)(可稱幾次)
#define FALSE -1
#define TRUE 0
int badBallWeight;
char badBall;
//得到壞球的字符
char getBadBall()
{
srand((unsigned)time(NULL));
return (char)(random()ºLL_NUM+LETTER_HEAD);
}
//得到壞球的重量
void getBadBallWeight()
{
int type;
srand((unsigned)time(NULL));
type = random()%2;
if(1==type)
{
badBallWeight = HIGH_WEIGHT;
}
else
{
badBallWeight = LOW_WEIGHT;
}
}
//開啟了debug后 返回FALSE
int debug(char* str)
{
if(strcmp(str, "exit") == 0)
{
exit(0);
}
if(strcmp(str, "cheat") == 0)
{
printf("CHEAT : bad ball is %c, weight is %d\n", badBall, badBallWeight);
return FALSE;
}
return TRUE;
}
//驗證球的名字,不在a-l之間的返回FALSE
int verifyBallsName(char* str)
{
char* p;
for(p = str; *p != ’\0’; p++)
{
if(*p<’a’ || *p>’l’)
{
return FALSE;
}
}
return TRUE;
}
#include
#include
#include
#define LETTER_HEAD 97 //a的起始
#define BALL_NUM 12 //球個數(shù)
#define NORMAL_WEIGHT 2 //一般球的重量
#define HIGH_WEIGHT 3 //球超重
#define LOW_WEIGHT 1 //球失重...
#define LOOP 3 //循環(huán)次數(shù)(可稱幾次)
#define FALSE -1
#define TRUE 0
int badBallWeight;
char badBall;
//得到壞球的字符
char getBadBall()
{
srand((unsigned)time(NULL));
return (char)(random()ºLL_NUM+LETTER_HEAD);
}
//得到壞球的重量
void getBadBallWeight()
{
int type;
srand((unsigned)time(NULL));
type = random()%2;
if(1==type)
{
badBallWeight = HIGH_WEIGHT;
}
else
{
badBallWeight = LOW_WEIGHT;
}
}
//開啟了debug后 返回FALSE
int debug(char* str)
{
if(strcmp(str, "exit") == 0)
{
exit(0);
}
if(strcmp(str, "cheat") == 0)
{
printf("CHEAT : bad ball is %c, weight is %d\n", badBall, badBallWeight);
return FALSE;
}
return TRUE;
}
//驗證球的名字,不在a-l之間的返回FALSE
int verifyBallsName(char* str)
{
char* p;
for(p = str; *p != ’\0’; p++)
{
if(*p<’a’ || *p>’l’)
{
return FALSE;
}
}
return TRUE;
}