#include<stdlib.h>
#include<time.h>
#include<string.h>
main()
{
int n[4],a,b,c,d,yes=0,meble=0
int times=10,y=0
srand(time(NULL))
int x[10]
memset(x, 0, sizeof(int)*10)
int i = 0
while (1)
{
if(i == 4)
break
int k = rand() % 10
if(x[k] == 0)
{
x[k] = 1
n[i] = k
i++
}
}
// printf("%d %d %d %d\n",n[0],n[1],n[2],n[3])
printf("猜数游戏:四个数0-9,按一定顺序排列,你需要在\n")
printf("10次机会内,猜中数字而且数的顺序要正确\n")
while(times!=0)
{
printf("请输入4个数(0-9)\n")
printf("数与数之间一个空格分开\n")
scanf("%d %d %d %d",&a,&b,&c,&d)
yes=0meble=0
if(n[0]==a)yes++
if(n[1]==b)yes++
if(n[2]==c)yes++
if(n[3]==d)yes++
if(a==n[1]||a==n[2]||a==n[3])meble++
if(b==n[0]||b==n[2]||b==n[3])meble++
if(c==n[0]||c==n[1]||c==n[3])meble++
if(d==n[0]||d==n[1]||d==n[2])meble++
printf("%d 个数完全正确, %d 个数值对但位置错了 \n",yes,meble)
y=y+1
printf("第 %d 次机会",y)
printf("\n")printf("\n")printf("\n")
times--
if(a==n[0]&&b==n[1]&&c==n[2]&&d==n[3])
{printf("恭喜 - -。 猪竟然都能赢\n")break}
if(times==0)
{printf("game over 了\n")break}
}
system("pause")
}
思路:随机产生4个不同的数(0-9),然后你输入4个数做比较,输出结果。
#!/bin/bashcp=$(( $RANDOM % 3 )) #获取0-2的随机数
read my
case $my in
0)
if [ $cp = 0 ]then
echo "您出石头,电脑出石头"
echo "结果:平局"
elif [ $cp = 1 ]then
echo "您出石头,电脑出剪刀"
echo "结果:恭喜您获胜!"
else
echo "您出石头,电脑出布"
echo "结果:很遗憾您输了"
fi
1)
if [ $cp = 0 ]then
echo "您出剪刀,电脑出石头"
echo "结果:很遗憾您输了"
elif [ $cp = 1 ]then
echo "您出剪刀,电脑出剪刀"
echo "结果:平局"
else
echo "您出剪刀,电脑出布"
echo "结果:恭喜您获胜"
fi
2)
if [ $cp = 0 ]then
echo "您出布,电脑出石头"
echo "结果:恭喜您获胜"
elif [ $cp = 1 ]then
echo "您出布,电脑出剪刀"
echo "结果:很遗憾您输了"
else
echo "您出布,电脑出布"
echo "结果:平局"
fi
*)
echo "您的输入有误,请重新开始"
exit 1
esac
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)