另类猜数字
答案:2 悬赏:0 手机版
解决时间 2021-11-26 20:16
- 提问者网友:浮克旳回音
- 2021-11-26 09:51
另类猜数字
最佳答案
- 五星知识达人网友:蓝房子
- 2021-11-26 10:21
#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
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:"<
{
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<
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."<
do
{
cout<<"Continue?(Y/N)"<
if(c=='Y' || c=='y')
{
cout<<"Think of a number,and tell me 'a b ' for my answer."<
}
}while(c!='n' && c!='N');
}
全部回答
- 1楼网友:夜余生
- 2021-11-26 10:33
#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.
}
#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.
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯