永发信息网

另类猜数字

答案:2  悬赏:0  手机版
解决时间 2021-11-26 20:16
另类猜数字
最佳答案
#include
#include
#include

struct RecordNode
{
int num;
int a;
int b;
int digit[4];
};

void Separate(RecordNode &Node)
{
Node.digit[0] = Node.num /1000;
Node.digit[1] = (Node.num /100)-(10*Node.digit[0]);
Node.digit[2] = (Node.num /10)-(100*Node.digit[0])-(10*Node.digit[1]);
Node.digit[3] = Node.num-(1000*Node.digit[0])-(100*Node.digit[1])-(10*Node.digit[2]);
}

void Compare(RecordNode Base,RecordNode &Test)
{//test a & b of Test.num,(compare with Base)
//suppose the nodes' num have been separated,and there are no same digits
Test.a=0;
Test.b=0;
// cout<<"base:"< for(int i=0;i<4;i++)
{
if(Base.digit[i]==Test.digit[i]) //get_A_part
{
Test.a++;
}
for( int j=0; j<4; j++) //get_B_part
{
if(j!=i)
{
if(Base.digit[i]==Test.digit[j])
{
Test.b++;

}
}
}//for(j..)
}//for(i..), the 4 digits in Base
// cout< }//Compare(Base,&Test)

int Repeated(RecordNode Node)
{//judge whether digit No.n(from 0) is the same as others
for(int outer=0; outer<3; outer++)
{
for(int inner=outer+1; inner<4; inner++)
{
if(Node.digit[outer]==Node.digit[inner])
{
return 1;
}
}
}
return 0;
}//Repeated(...)

void GuessNum()

{
int temp;

struct RecordNode Record[10];

struct RecordNode CurNum;

int step=1;

for(CurNum.num=0123; CurNum.num<=9876; CurNum.num++) //try possible ones

{

Separate(CurNum);

if(!Repeated(CurNum))

{

int RecordNum=0;

if(step!=1) //need to check records

{

RecordNum=step-1;

do

{

Separate(Record[RecordNum-1]);

Compare(Record[RecordNum-1],CurNum);

RecordNum--;

}while(RecordNum>0 && CurNum.a==Record[RecordNum].a

&& CurNum.b==Record[RecordNum].b);

}//if(step!=1)

if(step==1 || (CurNum.a==Record[RecordNum].a && CurNum.b==Record[RecordNum].b)) //possible answer

{

cout<<"step "<
if(CurNum.digit[0]==0) //begin with '0'

{

cout<<'0';

}

cout<
cin>>temp>>CurNum.a;
CurNum.b = temp - CurNum.a;

while(CurNum.a + CurNum.b >4) //illegal input

{

cout<<"check your answer!"<
cout<<"step "<
if(CurNum.digit[0]==0) //begin with '0'

{

cout<<'0';

}

cout<
cin>>temp>>CurNum.a;

CurNum.b = temp - CurNum.a;

}

if(CurNum.a==4 && CurNum.b==0) //suppose type correctly,not check records

{

cout<<"Got it! ^-^"<
return; //needn't record the final answer!

}

else //it's not the answer,record it

{

Record[step-1]=CurNum;

step++;

}

}//if(... it may be the answer)

if(step>10)

{

cout<<"Fail to find it!"<
}

}//if(!Repeated)

}//for(CurNum.num...)

if( step<10 ) //can't get any number in 0123 ~ 9876
{
cout<<"Are you sure your answer's right?"< }
}//GuessNum()

void main(void)
{
cout<<"I'm going to guess the number you get in your mind."< char c;
do
{
cout<<"Continue?(Y/N)"< cin>>c;
if(c=='Y' || c=='y')
{
cout<<"Think of a number,and tell me 'a b ' for my answer."< GuessNum();
}
}while(c!='n' && c!='N');
}
全部回答
#include
#include
#include

struct right_place {int right. int place.}.

int main (void)
{
int creatDialog (int x, int *p ).

void rndProduce (int numDft[]).
struct right_place numCompare (int numDft[], int numIn[]).

int dialog, quitOrContinue, allQuit.
int numDft[4], numIn[4], result[2].
struct right_place compareBack.

creatDialog (1, NULL).

allQuit = 1.
while (allQuit == 1)
{
creatDialog (7, NULL).
rndProduce (numDft).
quitOrContinue = 1.
while (quitOrContinue == 1)
{
dialog = 2.
while (dialog == 2)
{
creatDialog (2, numIn).
dialog = creatDialog (3, numIn).
}
compareBack = numCompare (numDft, numIn).
result[0] = compareBack.right .
result[1] = compareBack.place .
quitOrContinue = creatDialog (4, result).
}
if (quitOrContinue == 2)
allQuit = creatDialog (5, numDft).
if (quitOrContinue == 3)
allQuit = 1.
if (quitOrContinue == 4)
allQuit = 2.
}

if (allQuit == 2)
creatDialog (6, NULL).

getchar ().
return 0.
}

int creatDialog (int x, int *p)
{
int back.
int i.
switch (x)
{
case 0:
printf ("************************************************************************\n").
break.
case 1:
creatDialog (0, NULL).
printf ("欢迎玩猜数字游戏!\n版本2.0\n作者:andyzhshg\n制作日期:2007.9.1\n").
creatDialog (0, NULL).
printf ("在本游戏中,你需要输入四个数字,\n然后电脑会给出你猜测的正确情况。").
printf ("电脑会用它之前生成的\n四位数与之你的输入比较,然后告诉你").
printf ("你猜对了几个数字,包\n括数值和顺序的信息.\n").
break.
case 2:
creatDialog (0, NULL).
printf ("请输入你猜测的四位数字(用空格、回车、或Tab隔开):\n").
for (i = 0. i < 4. i , p )
scanf ("%i", p).
break.
case 3:
creatDialog (0, NULL).
printf ("你输入的数字是:%i %i %i %i ,确认请输入“1”,重新输入按“2”。\n", *p, *(p 1), *(p 2), *(p 3)).
scanf ("%i", &.back).
return back.
break.
case 4:
creatDialog (0, NULL).
printf ("你输入的数字中:\n数值和位置都正确的有%i个,\n数值正确但位置不正确的有%i个.\n", *p, *(p 1)).
if ((*p) == 4)
{
printf ("恭喜你,答对了!\n").
printf ("继续游戏吗?继续请输入“1”,退出游戏请输入“2”。\n").
scanf ("%i", &.back).
if (back == 1)
return 3.
if (back == 2)
return 4.
}
else
{
printf ("继续吗?继续请输入“1”,显示正确答案请输入“2”。\n").
scanf ("%i", &.back).
return back.
}
break.
case 5:
creatDialog (0, NULL).
printf ("正确答案是:%i %i %i %i,再接再厉啊!\n", *p, *(p 1), *(p 2), *(p 3)).
printf ("继续游戏吗?继续请输入“1”,退出游戏请输入“2”。\n").
scanf ("%i", &.back).
return back.
break.
case 6:
creatDialog (0, NULL).
printf ("感谢你玩本游戏,再见!\n按任意键退出\n").
break.
case 7:
creatDialog (0, NULL).
printf ("新的游戏开始了!GOOD LUCK!\n").
break.
default:
break.
}
}

void rndProduce (int numDft[])
{
int i.
srand( (unsigned)time( NULL ) ) .
for ( i = 0. i < 4. i )
numDft[i] = rand () % 10.
//printf ("%i %i %i %i\n", numDft[0], numDft[1], numDft[2], numDft[3]).
}

struct right_place numCompare (int numDft[], int numIn[])
{
int i, j.
struct {int Dft. int In.} flag[4] = { 1, 1, 1 ,1 ,1, 1, 1, 1 }.
struct right_place result = { 0, 0 }.
for (i = 0. i < 4. i )
if (numDft[i] == numIn[i])
{
(result.right) .
flag[i].Dft = 0.
flag[i].In = 0.
}
for (i = 0. i < 4. i )
for (j = 0. j < 4. j )
if (flag[i].Dft &.&. flag[j].In &.&. numDft[i] == numIn[j])
{
(result.place) .
flag[i].Dft = 0.
flag[j].In = 0.
}
return result.
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
我整天学习,感觉太累了,看不到希望
家有半新冰箱想卖掉怎么办
新广通汽车维修中心地址在什么地方,我要处理
5%的硼酸溶液怎么配成2%~3%的硼酸溶液?
如果 Linux里的 VI编辑器中 按什么键 让命令
陕西盛远建筑装饰工程有限公司到底存不存在?
求离殇小五歌曲
为什么有的人当了几年还是一个兵,当兵怎样才
新南枫汽车修配厂地址有知道的么?有点事想过
无锡国宏精密待遇
1KB等于多少GB?等于多少M?
做梦梦见在晚上偷偷逃出学校走一条夜路,路上
夏季三伏天,如何护肤
STC动力节油卡安装简单吗?
求教im70耳套问题
推荐资讯
苏州金门国际商业广场的商业规划
家用空气加湿机怎么把水瞬间变成汽!
髟加皿和哀字没一点是什么字?
银色zippo jimbeam 是哪个系列的?
中央台主持人张宇身高
磐石混凝土公司在什么地方啊,我要过去处理事
拍下商品后,商品下架怎么回事
怎么用膜分离法除去甲醇中的水,原理是什么?
火加斤是什么字?
燕市哭歌悲遇合,秦淮风月忆繁华.新愁旧恨知
用简便方法计算五年级6.4除以0.25
我国的地名用—川、潼、梓等字做地名,是什么
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?