+ -
当前位置:首页 → 问答吧 → C 写简单板Mastermind 问题

C 写简单板Mastermind 问题

时间:2014-03-21

来源:互联网

Guess the three colors sequence: 0 1 0

0 correct color at the correct position
2 correct color at the incorrect positions

The correct sequence of colors is 1 0 2

如果要做到噤
我想知系correct color at the incorrect positions部分
应该点用ELSE IF 去写?

作者: a002305   发布时间: 2014-03-21

引用:原帖由 a002305 於 2014-2-28 07:57 PM 发表
Guess the three colors sequence: 0 1 0

0 correct color at the correct position
2 correct color at the incorrect positions

The correct sequence of colors is 1 0 2

如果要做到噤
我想知系correc ...
首先可抽出“correct color at the correct position”的,如中的把 guess 及 answer 相关位置改成 -1 。
跟着对每种 color 数 guess 及 answer 中各有多少个,如 guessColor[0]=2 , guessColor[1]=1 , guessColor[2]=0 , answerColor[0]=1 , answerColor[1]=1 , answerColor[2]=1 。
余下的应该想到吧!

作者: xianrenb   发布时间: 2014-03-21

这是ARRAY 吗? 如果只用IF 跟 ELSE If 能写出来吗

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
int color1, color2, color3; // the sequence of colors

// Generate the random sequence
srand ( time(NULL) );
color1 = rand() % 3;
color2 = rand() % 3;
color3 = rand() % 3;

// Add your declarations & statements here
int num1 , num2 , num3;
int count1 = 0;
int count2 = 0;

printf("Guess the three colors sequence:");
scanf("%d %d %d" , &num1 , &num2 , &num3);

{
if(num1 == color1)
count1++;
else if(num1 == color2 || num1 == color3)
count2++;
}

{
if(num2 == color2)
count1++;

else if (num2 == color1 || num2 == color3)
count2++;
}

{
if(num3 == color3)
count1++;
else if(num3 == color1 || num3 == color2)
count2++;
}
以上是我打的
ran 部份是老师给的
但总是排除不了 如果是对色对位的 不计算在对色错位的部分
如果不用ARRAY 还有其他方法做吗 感谢师兄
(PS 不想改动老师给予的RAN)

作者: a002305   发布时间: 2014-03-21

引用:原帖由 a002305 於 2014-3-1 03:00 PM 发表
这是ARRAY 吗? 如果只用IF 跟 ELSE If 能写出来吗

#include
#include
#include

int main(void)
{
int color1, color2, color3; // the sequence of colors

// Generate the ...
是 array 。
类似的 variable 最好都是组成 array 来写比较好。
因为很多时可以利用 loop 来写较为简单的 code 。
当然分开多个 variable 来写亦可,没有分别的。

其实我初看这个问题,我自己都想错了方法。
但好多时应付写程式的问题,只要用最笨的方法,一步一步的把问题按最普通的 step 去做,就能解决。
如这问题,首先要搞清楚“correct color at the correct position”及“correct color at the incorrect positions”。
一定是先计算前者,后计算后者。
是前者的颜色,就一定不是后者的颜色。

写程式计算时可以用几多个 variable 、 array 等都得。
如果要 keep 住原来的 data ,大可 copy 至新增的 variable / array 。

作者: xianrenb   发布时间: 2014-03-21

#include <stdio.h>
#include <stdlib.h> // for functions srand() & rand()
#include <time.h> // for function time()

int main(void)
{
int color1, color2, color3; // the sequence of colors

// Generate the random sequence
srand ( time(NULL) );
color1 = rand() % 3;
color2 = rand() % 3;
color3 = rand() % 3;

// Add your declarations & statements here
int guessNum1 , guessNum2 , guessNum3;
int countNum1 , countNum2;
int check1 , check2 , check3;

countNum1 = 0;
countNum2 = 0;

check1 = 1;
check2 = 1;
check3 = 1;

printf("Guess the three colors sequence:");
scanf("%d %d %d" , &guessNum1 , &guessNum2 , &guessNum3);

if(guessNum1 == color1)
{
countNum1++;
check1--;
}

if(guessNum2 == color2)
{
countNum1++;
check2--;
}

if(guessNum3 == color3)
{
countNum1++;
check3--;
}

if(check1 > 0 && guessNum1 == color2 || guessNum1 == color3)
{
countNum2++;
}

if(check2 > 0 && guessNum2 == color1 || guessNum2 == color3)
{
countNum2++;
}

if(check3 > 0 && guessNum3 == color1 || guessNum3 == color2)
{
countNum2++;
}

printf("%d correct color at the correct position\n" , countNum1);
printf("%d correct color at the incorrect position\n" , countNum2);
printf("The correct sequence of colors is %d %d %d\n" , color1 , color2 , color3);

system("pause");

return 0;

}

我改成这样 但也不成功 请教各位师兄><

作者: a002305   发布时间: 2014-03-21

(check1 > 0 && guessNum1 == color2 || guessNum1 == color3)

这一段中
我是想表达 check1 > 0 and guessNum1 = color2 or guessNum1 = color3
有需要写成
if((check1 > 0 && guessNum1 == color2) || (check1 > 0 && guessNum1 == color3)) 吗?

作者: a002305   发布时间: 2014-03-21

你首先要知道以下两句嘅分别:
复制内容到剪贴板代码:if (a == 0 && b == 1 || c == 2)
...
if ((a == 0 && b == 1) || (a == 0 && c == 2)
...
唔知嘅话,睇书或者用 google 搵资料,然后写段程式码去验证。
重有,逻辑运算子 && 同 ||,哪个优先?

作者: fitcat07   发布时间: 2014-03-21

谢谢师兄提点!

作者: a002305   发布时间: 2014-03-21