人机猜数游戏(C语言)

weixin_45941689 2019-12-06 01:38:59
由计算机“想”一个四位数,请人让你猜这个四位数是多少,人输入这个四位数后。计算机首先判断这四个数中几个猜对了,并且猜对的数字中有几个位置也猜对的,讲结果显示出来,给人提示,请人再猜,直到猜出计算机想的四位数为止。请编程实现该游戏,游戏结束时,显示人才一个数用了几次,提示:用库函数random()产生一个随机数。 int z; z=random(9999)
...全文
206 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuangbao9 2019-12-10
  • 打赏
  • 举报
回复
引用 2 楼 weixin_45941689 的回复:
[quote=引用 1 楼 kuangbao9的回复:]#include<stdio.h> #include<stdlib.h> #include<random> #include<time.h> int main() { int rad; int num,count=0; int arr_rad[4], arr_num[4]; int correct_num = 0, correct_ind=0; srand((unsigned)time(NULL));//用当前时间,设置种子 while (1) { rad = rand(); if (rad < 1000 || rad > 9999) continue; printf("%d\n", rad); //生成随机数 break; } while (1) { printf("请输入一个四位数: "); scanf_s("%d", &num); arr_num[0] = num / 1000; arr_num[1] = num % 1000 / 100; arr_num[2] = num % 1000 % 100 / 10; arr_num[3] = num % 10; arr_rad[0] = rad / 1000;//因为下面将其重新赋值-1,所以这里需要再次赋值 arr_rad[1] = rad % 1000 / 100; arr_rad[2] = rad % 1000 % 100 / 10; arr_rad[3] = rad % 10; count++; if (rad == num) { printf("猜中,使用次数: %d\n", count); break; } else { for (int i = 0; i < 4; i++) { if (arr_num[i] == arr_rad[i]) //先判断位置对的数字 correct_ind++; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (arr_num[i] == arr_rad[j]) //判断数字对的数字 { arr_rad[j] = -1; //避免重复计数,当查到数字一样时,赋值-1,这样当有同样数字时就不会重复计数 correct_num++; break; } } } printf("你猜对了 %d 个数字,有 %d 个数字位置正确\n", correct_num, correct_ind); correct_num = 0; correct_ind = 0; } } system("pause"); return 0; } 仅供参考
第二行#include<stdlib.h>这里显示错误[/quote] 那你把它注释,然后把最后一行的system("pause")去掉
weixin_45941689 2019-12-10
  • 打赏
  • 举报
回复
引用 1 楼 kuangbao9的回复:
#include<stdio.h> #include<stdlib.h> #include<random> #include<time.h> int main() { int rad; int num,count=0; int arr_rad[4], arr_num[4]; int correct_num = 0, correct_ind=0; srand((unsigned)time(NULL));//用当前时间,设置种子 while (1) { rad = rand(); if (rad < 1000 || rad > 9999) continue; printf("%d\n", rad); //生成随机数 break; } while (1) { printf("请输入一个四位数: "); scanf_s("%d", &num); arr_num[0] = num / 1000; arr_num[1] = num % 1000 / 100; arr_num[2] = num % 1000 % 100 / 10; arr_num[3] = num % 10; arr_rad[0] = rad / 1000;//因为下面将其重新赋值-1,所以这里需要再次赋值 arr_rad[1] = rad % 1000 / 100; arr_rad[2] = rad % 1000 % 100 / 10; arr_rad[3] = rad % 10; count++; if (rad == num) { printf("猜中,使用次数: %d\n", count); break; } else { for (int i = 0; i < 4; i++) { if (arr_num[i] == arr_rad[i]) //先判断位置对的数字 correct_ind++; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (arr_num[i] == arr_rad[j]) //判断数字对的数字 { arr_rad[j] = -1; //避免重复计数,当查到数字一样时,赋值-1,这样当有同样数字时就不会重复计数 correct_num++; break; } } } printf("你猜对了 %d 个数字,有 %d 个数字位置正确\n", correct_num, correct_ind); correct_num = 0; correct_ind = 0; } } system("pause"); return 0; } 仅供参考
第二行#include<stdlib.h>这里显示错误
kuangbao9 2019-12-06
  • 打赏
  • 举报
回复
#include<stdio.h> #include<stdlib.h> #include<random> #include<time.h> int main() { int rad; int num,count=0; int arr_rad[4], arr_num[4]; int correct_num = 0, correct_ind=0; srand((unsigned)time(NULL));//用当前时间,设置种子 while (1) { rad = rand(); if (rad < 1000 || rad > 9999) continue; printf("%d\n", rad); //生成随机数 break; } while (1) { printf("请输入一个四位数: "); scanf_s("%d", &num); arr_num[0] = num / 1000; arr_num[1] = num % 1000 / 100; arr_num[2] = num % 1000 % 100 / 10; arr_num[3] = num % 10; arr_rad[0] = rad / 1000;//因为下面将其重新赋值-1,所以这里需要再次赋值 arr_rad[1] = rad % 1000 / 100; arr_rad[2] = rad % 1000 % 100 / 10; arr_rad[3] = rad % 10; count++; if (rad == num) { printf("猜中,使用次数: %d\n", count); break; } else { for (int i = 0; i < 4; i++) { if (arr_num[i] == arr_rad[i]) //先判断位置对的数字 correct_ind++; } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (arr_num[i] == arr_rad[j]) //判断数字对的数字 { arr_rad[j] = -1; //避免重复计数,当查到数字一样时,赋值-1,这样当有同样数字时就不会重复计数 correct_num++; break; } } } printf("你猜对了 %d 个数字,有 %d 个数字位置正确\n", correct_num, correct_ind); correct_num = 0; correct_ind = 0; } } system("pause"); return 0; } 仅供参考

65,170

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧