Russian roulette, many of you have heard of it, is a cruel game. The game’s prop is a revolver, and the rules are simple: put one or more bullets in one of the six slots of the revolver, rotate the wheel at will, and close the wheel. Participants in the game take turns pointing a pistol at them and pulling the trigger: hit or stagefright, the loser; The one who holds out to the end is the winner.

This practice project is similar to roulette, the rules of the game: N participants in a ring, each time the host to the revolver loaded a bullet, and randomly turn to close the wheel, the game starts from the first person, take turns to take the gun; The person who is shot quits the table, and the next person who quits starts the next round as the first person. Until there is only one person left, the winner. Requirements: Simulate the rules of the game of roulette, find the final winner of the game.

Design ideas

To solve similar problems, the use of linear table sequential storage structure and chain storage structure can be achieved, according to the rules of the game, in the use of chain storage structure only need to use circular linked list can easily solve the problem. Sequential storage structure simulation roulette using the sequential storage structure, also want to connect the beginning and end of the array in mind, that is, when the need from the last position in the array to find the next position, to be able to jump to the first position of the array.

#include <stdio.h> #include <stdlib.h> #include <time.h> typedef enum {false,true} bool; typedef struct line{ int No; struct line * next; }line; Void initLine(line ** head,int n){*head=(line*)malloc(sizeof(line)); void initLine(line ** head,int n); (*head)->next=NULL; (*head)->No=1; line * list=*head; for (int i=1; i<n; i++) { line * body=(line*)malloc(sizeof(line)); body->next=NULL; body->No=i+1; list->next=body; list=list->next; } list->next=*head; Void display(line * head){line * temp=head; while (temp->next! =head) { printf("%d ",temp->No); temp=temp->next; } printf("%d\n",temp->No); } int main() { line * head=NULL; srand((int)time(0)); int n,shootNum,round=1; Printf (" Enter number of gamblers: "); scanf("%d",&n); initLine(&head,n); line* lineNext=head; // Exit the loop while (head->next!) only if there is only one node in the list, i.e. the head node. =head) {printf(" start at %d, start at %d, ",round, linenext-> No); shootNum=rand()%n+1; Printf (" The gun goes off at %d pull of the trigger \n",shootNum); line *temp=lineNext; For (int I =1; int I =1; i<shootNum-1; i++) { temp=temp->next; } // Delete the node to be deleted from the list and release its space printf(" The gambler numbered %d exits, the remaining gamblers numbered in order: \n",temp->next->No); line * del=temp->next; temp->next=temp->next->next; if (del==head) { head=head->next; } free(del); display(head); LineNext =temp->next; round++; } printf(" The final winning gambler's number is: %d\n",head->No); return 0; }