C 写简单板Mastermind 问题
时间:2014-03-21
来源:互联网
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
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 ...
跟着对每种 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

#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
这是ARRAY 吗? 如果只用IF 跟 ELSE If 能写出来吗

#include
#include
#include
int main(void)
{
int color1, color2, color3; // the sequence of colors
// Generate the ...
类似的 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 <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 and guessNum1 = color2 or guessNum1 = color3
有需要写成
if((check1 > 0 && guessNum1 == color2) || (check1 > 0 && guessNum1 == color3)) 吗?
作者: a002305 发布时间: 2014-03-21
...
if ((a == 0 && b == 1) || (a == 0 && c == 2)
...
重有,逻辑运算子 && 同 ||,哪个优先?
作者: fitcat07 发布时间: 2014-03-21

作者: a002305 发布时间: 2014-03-21
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28