C 100囚犯谜语,我的代码有问题
(如果您已经知道谜语是什么,只是阅读最后2行)
我会看到一个有关谜语的视频,该视频称为“ 100名囚犯谜语”,它实际上告诉您一群囚犯(一次只有一个人)进入一个房间,这个房间的盒子的盒子从1到100订购,但盒子里的数字是随机的,每个囚犯也进入房间的数量也从1到100,因此每个囚犯都必须选择盒子那有他的数字,如果每个囚犯打开50个盒子,他没有发现自己的电话号码,则每人都有一系列尝试(50次尝试)!例如,第1名囚犯进入房间,他必须找到具有他的电话号码的盒子。.可能是7号或19或27的框!所以这只是一个运气的游戏..还是是?该游戏具有数学上解决难题的策略和方法问题,谢谢大家:)!
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j = 0, k = 0, counter = 0;
int boxes[10];
int boxEntered;
for (i = 0; i <= 10; i++) \\ numbering the array
boxes[i] = i;
for (i = 0; i <= 10; i++) {
int temp = boxes[i];
int randomIndex = (rand() % 10); \\ shuffling the boxes to put random numbers
boxes[i] = boxes[randomIndex];
boxes[randomIndex] = temp;
}
for (i = 0; i <= 10; i++) {
printf("%d : (%d)\n", boxes[i], i); \\ print the boxes randomized and their index ordered
}
printf("You only have 5 tries!\n");
while (k != 5) {
while (j < 10) {
printf("Pick a box number between 0 and 10 (You are number %d)\n",counter);
scanf("%d",&boxEntered);
if (boxes[boxEntered] == boxes[counter]) {
printf("\nYou succeded, PROCEED TO NEXT PRISONER\n");
j++; \\ go to the next iteration
k = 0; \\ set tries back to 0
counter++;
} else
printf("Try again\nThe box you entered had number %d\n",boxes[boxEntered]);
k++;
if (k == 5) { \\ if player prisoner fails 5 times you break the loop
break;
}
}
}
if (counter == 10) { \\ if last prisoner was reached successfully then game is won
printf("You are freed!");
} else {
printf("You are going back heheheheheh!\n")
}
return 0;
}
如您在这张图中所见,输出根本没有任何意义,我不知道这里有什么问题。
(If you already know what the riddle is about just read the last 2 lines)
I saw a video about a riddle which is called "The 100 prisoners riddle" it essentially tells you that a bunch of prisoners (only one person at a time) get into a room, this room has boxes that are ordered correctly from 1 to a 100 but the numbers inside the boxes are random and each prisoner getting into the room is numbered from 1 to a 100 too, so each prisoner has to pick the box that has his number, each prisoner has a set of tries (50 tries) if he opened 50 boxes and he didn't find his number he loses! for example prisoner number 1 gets in the room and he has to find the box that has his number .. it might be box number 7 or 19 or 27 who knows! so it's just a game of luck .. or is it? the game has strategies and ways to mathematically solve the puzzle but that's not my problem here, I just wanna program the game in C and solve the puzzle for myself, the code has a lot of holes in it so look closely into it and find what's the problem, THANK YOU ALL :)!
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, j = 0, k = 0, counter = 0;
int boxes[10];
int boxEntered;
for (i = 0; i <= 10; i++) \\ numbering the array
boxes[i] = i;
for (i = 0; i <= 10; i++) {
int temp = boxes[i];
int randomIndex = (rand() % 10); \\ shuffling the boxes to put random numbers
boxes[i] = boxes[randomIndex];
boxes[randomIndex] = temp;
}
for (i = 0; i <= 10; i++) {
printf("%d : (%d)\n", boxes[i], i); \\ print the boxes randomized and their index ordered
}
printf("You only have 5 tries!\n");
while (k != 5) {
while (j < 10) {
printf("Pick a box number between 0 and 10 (You are number %d)\n",counter);
scanf("%d",&boxEntered);
if (boxes[boxEntered] == boxes[counter]) {
printf("\nYou succeded, PROCEED TO NEXT PRISONER\n");
j++; \\ go to the next iteration
k = 0; \\ set tries back to 0
counter++;
} else
printf("Try again\nThe box you entered had number %d\n",boxes[boxEntered]);
k++;
if (k == 5) { \\ if player prisoner fails 5 times you break the loop
break;
}
}
}
if (counter == 10) { \\ if last prisoner was reached successfully then game is won
printf("You are freed!");
} else {
printf("You are going back heheheheheh!\n")
}
return 0;
}
As you can see in this picture the output doesn't make any sense at all and i have no idea what is wrong here..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的代码逻辑中,您应该
以此
替换是因为
Counter
这里似乎代表了囚犯。服用box [counter]
将为您提供 box ,这不是您想要的;您正在尝试查看盒子是否与当前的囚犯相匹配。另一个重要的说明是,以下代码将超出您的数组的界限,从而导致不确定的行为:
box
被声明为具有大小10
,因此请使用boxes [ 10]
超出界限;最大值是框[9]
。要解决此问题,您可以从
1
开始索引数组。为了在c
中执行此操作,而不是声明box [10]
,使用box [11]
。这将确保您可以访问框[10]
。然后,您可以将循环更改为从
1
开始,因此类似:确保为每个数组和代码中的循环进行此更改。
From your code's logic, you should replace
with
This is because
counter
here seems to represent a prisoner. Takingboxes[counter]
will give you a box, which isn't what you want; you're trying to see if the box matches the current prisoner.Another important note is the following code will go out of bounds for your array, causing undefined behaviour:
boxes
is declared as having size10
, and therefore takingboxes[10]
goes out of bounds; the maximum isboxes[9]
.To fix this, you can index your arrays starting from
1
. To do this inC
, instead of declaringboxes[10]
, useboxes[11]
. This will ensure you can accessboxes[10]
.You can then change your loops to start from
1
, so something like:Be sure to make this change for every array and for loop in your code.
干得好。我已经对您的程序进行了一些编辑(实际上是一个很棒的程序! - 我喜欢它!)。我只进行了一些小更改,但确实有效(对于一件事,我添加了
srand()
函数,以便您的程序每次都会产生不同的结果。此外,主要罪魁祸首是行:如果(boxes [boxentered] == boxes [counter])
应为if(boxes [boxentered] == counter)
。Here you go. I have done some editing to your program (it's actually a great program! - I love it!). I've only made some minor changes but it does work (for one thing I have added the
srand()
function so that your program will produce a different result each time. Also, the main culprit is that the line:if (boxes[boxEntered] == boxes[counter])
should beif (boxes[boxEntered] == counter)
.这是您的程序组织到功能中。请注意,有一些非常重的编辑。一方面,您的语句(k!= 5)是不必要的,因此已将其删除(删除循环需要搬迁您的某些语句)。
Here is your program organized into functions. Please note that there was some pretty heavy editing. For one thing your statement while (k != 5) was unnecessary so it was removed (removing the loop entailed the relocation of some of your statements).