C 100囚犯谜语,我的代码有问题

发布于 2025-02-12 23:05:37 字数 2074 浏览 3 评论 0原文

(如果您已经知道谜语是什么,只是阅读最后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..

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呢古 2025-02-19 23:05:37

从您的代码逻辑中,您应该

boxes[boxEntered] == boxes[counter]

以此

boxes[boxEntered] == counter

替换是因为Counter这里似乎代表了囚犯。服用box [counter]将为您提供 box ,这不是您想要的;您正在尝试查看盒子是否与当前的囚犯相匹配。

另一个重要的说明是,以下代码将超出您的数组的界限,从而导致不确定的行为:

for (i = 0; i <= 10; i++) boxes[i] = i;

box被声明为具有大小10,因此请使用boxes [ 10]超出界限;最大值是框[9]

要解决此问题,您可以从1开始索引数组。为了在c中执行此操作,而不是声明box [10],使用box [11]。这将确保您可以访问框[10]

然后,您可以将循环更改为从1开始,因此类似:

for (i = 1; i <= 10; i++) boxes[i] = i;

确保为每个数组和代码中的循环进行此更改。

From your code's logic, you should replace

boxes[boxEntered] == boxes[counter]

with

boxes[boxEntered] == counter

This is because counter here seems to represent a prisoner. Taking boxes[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:

for (i = 0; i <= 10; i++) boxes[i] = i;

boxes is declared as having size 10, and therefore taking boxes[10] goes out of bounds; the maximum is boxes[9].

To fix this, you can index your arrays starting from 1. To do this in C, instead of declaring boxes[10], use boxes[11]. This will ensure you can access boxes[10].

You can then change your loops to start from 1, so something like:

for (i = 1; i <= 10; i++) boxes[i] = i;

Be sure to make this change for every array and for loop in your code.

只为守护你 2025-02-19 23:05:37

干得好。我已经对您的程序进行了一些编辑(实际上是一个很棒的程序! - 我喜欢它!)。我只进行了一些小更改,但确实有效(对于一件事,我添加了srand()函数,以便您的程序每次都会产生不同的结果。此外,主要罪魁祸首是行:如果(boxes [boxentered] == boxes [counter])应为if(boxes [boxentered] == counter)

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

int main()
  {
  int i, j = 0, k = 0, counter = 0;
  int boxes[10];
  int boxEntered;
  int temp;
  int randomIndex;
  time_t now;//Declaring time-type variable.

  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.

  for (i = 0; i < 10; i++) // numbering the array
    { boxes[i] = i; }

  for (i = 0; i < 10; i++)
    {
    temp = boxes[i];
    randomIndex = (rand() % 10); // shuffling the boxes to put random numbers
    boxes[i] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }

  for (i = 0; i < 10; i++)
    {
    printf("Box# %d =  %d\n", i, boxes[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] == 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!");
       break;
       }
    else
      { printf("You are going back heheheheheh!\n"); }
     }
  return 0;
  }


    

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 be if (boxes[boxEntered] == counter).

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

int main()
  {
  int i, j = 0, k = 0, counter = 0;
  int boxes[10];
  int boxEntered;
  int temp;
  int randomIndex;
  time_t now;//Declaring time-type variable.

  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.

  for (i = 0; i < 10; i++) // numbering the array
    { boxes[i] = i; }

  for (i = 0; i < 10; i++)
    {
    temp = boxes[i];
    randomIndex = (rand() % 10); // shuffling the boxes to put random numbers
    boxes[i] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }

  for (i = 0; i < 10; i++)
    {
    printf("Box# %d =  %d\n", i, boxes[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] == 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!");
       break;
       }
    else
      { printf("You are going back heheheheheh!\n"); }
     }
  return 0;
  }


    
泪冰清 2025-02-19 23:05:37

这是您的程序组织到功能中。请注意,有一些非常重的编辑。一方面,您的语句(k!= 5)是不必要的,因此已将其删除(删除循环需要搬迁您的某些语句)。

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

void seedRandom();
void numbering();
void shuffling();
void printShuffle();
void pickBox();

int boxes[10];

int main()
  {
  seedRandom();
  numbering();
  shuffling();
  printShuffle();
  pickBox();
  return 0;
  }

void seedRandom()
  {
  time_t now;//Declaring time-type variable.
  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.
  }

void numbering() // numbering the array
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { boxes[cntr] = cntr; }
  }

void shuffling() // shuffling the boxes to put random numbers
  {
  int cntr;
  int temp;
  int randomIndex;
  for (cntr = 0; cntr < 10; cntr++)
    {
    temp = boxes[cntr];
    randomIndex = (rand() % 10);
    boxes[cntr] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }
  }

void printShuffle()
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { printf("Box# %d =  %d\n", cntr, boxes[cntr]); } // print the boxes
                                       //randomized and their index ordered
  }

void pickBox()
  {
  int tries=0, prsnrNumb=0, boxEntered;
  printf("You only have 5 tries!\n");
  while (1)//Infinite loop
    {
    printf("Pick a box number between 0 and 9. You are number %d)\n",prsnrNumb);
    scanf("%d",&boxEntered);
    if (boxes[boxEntered] == prsnrNumb)
      {
      puts("You succeed!");
      if(prsnrNumb<9)
        {puts("PROCEED TO NEXT PRISONER\n");}
      prsnrNumb++; // go to the next iteration
      tries = 0; // set tries back to 0.
      }
      else
        {
        printf("Try again\nThe box you entered had number");
        printf("%d\n",boxes[boxEntered]);
        tries++;
        if (tries == 5)//if player prisoner fails 5 times you break the loop
          {
          printf("You are going back heheheheheh!\n");
          break;
          }
        }
    if (prsnrNumb == 10) //if last prisoner was reached successfully then
                            //game is won
         {
         printf("You are freed!");
         break;
         }
      }
  }

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).

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

void seedRandom();
void numbering();
void shuffling();
void printShuffle();
void pickBox();

int boxes[10];

int main()
  {
  seedRandom();
  numbering();
  shuffling();
  printShuffle();
  pickBox();
  return 0;
  }

void seedRandom()
  {
  time_t now;//Declaring time-type variable.
  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.
  }

void numbering() // numbering the array
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { boxes[cntr] = cntr; }
  }

void shuffling() // shuffling the boxes to put random numbers
  {
  int cntr;
  int temp;
  int randomIndex;
  for (cntr = 0; cntr < 10; cntr++)
    {
    temp = boxes[cntr];
    randomIndex = (rand() % 10);
    boxes[cntr] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }
  }

void printShuffle()
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { printf("Box# %d =  %d\n", cntr, boxes[cntr]); } // print the boxes
                                       //randomized and their index ordered
  }

void pickBox()
  {
  int tries=0, prsnrNumb=0, boxEntered;
  printf("You only have 5 tries!\n");
  while (1)//Infinite loop
    {
    printf("Pick a box number between 0 and 9. You are number %d)\n",prsnrNumb);
    scanf("%d",&boxEntered);
    if (boxes[boxEntered] == prsnrNumb)
      {
      puts("You succeed!");
      if(prsnrNumb<9)
        {puts("PROCEED TO NEXT PRISONER\n");}
      prsnrNumb++; // go to the next iteration
      tries = 0; // set tries back to 0.
      }
      else
        {
        printf("Try again\nThe box you entered had number");
        printf("%d\n",boxes[boxEntered]);
        tries++;
        if (tries == 5)//if player prisoner fails 5 times you break the loop
          {
          printf("You are going back heheheheheh!\n");
          break;
          }
        }
    if (prsnrNumb == 10) //if last prisoner was reached successfully then
                            //game is won
         {
         printf("You are freed!");
         break;
         }
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文