Algorithm data structure: Monkey King

Posted by fatherlyons on Sat, 08 Jan 2022 17:26:32 +0100

[problem description]

Task: a bunch of monkeys have numbers. The numbers are 1, 2, 3 M, the group of monkeys (m) sit in a circle in the order of 1--m. count from the first to the nth, and the monkey will leave the circle. In this order, until there is only the last monkey left in the circle, the monkey is the king. Input data: enter m,n. m. N is an integer, n < m , output form: Chinese prompt: according to the method of M monkeys and counting n numbers, the number of monkeys output as king is. Establish a function to realize this function.

[other requirements]

(1) The naming of variables and functions conforms to the specification.

(2) Detailed notes: each variable requires notes to explain its purpose; The function has the function of annotation, and the purpose of parameters and return values should also be explained in the form of annotation; Key sentence segments require annotation interpretation.

(3) The level of the program is clear and readable.

(4) Beautiful interface and convenient interaction.

 

1, Data structure description

I use the dynamic array data structure. I use the array deletion method to perform a forward overwrite operation every n elements, and then push the count forward by one bit to continue the deletion operation of the elements until there is only one element left for output.

Visual interface I use easyx graphical interface tool.

 

      int *a = new int[m];//Set dynamic array

      for (int i = 0; i < m; i++) {//Assign values to dynamic arrays

             a[i] = i + 1;
      }

while (true) {

           if (m == 1)//Exit when there is only one element left

                  break;

           for (int i = (num - 1 + n) % m; i < m - 1; i++) {//Circular deletion interval element

                  a[i] = a[i + 1];//Forward coverage
           }

           num = (num - 1 + n) % m;

           m--;
    }
    return a[0];//Return monkey king element

 

2, Algorithm design

 

 

1. Flow chart of Monkey King program

First, build the start interface. After entering the game, input m and N. after converting to numbers, use m to set the dynamic array and assign values. Delete the array elements according to the interval n, that is, move the elements forward until there is only one element left. Set the result interface, output the results, and the program ends. If there is an input error, it will enter the error interface and exit the program.

 

3, Detailed design

#include <graphics.h>// Drawing library header file, drawing statement needs
#include <conio.h>// Console I / O header file, getch()Statement needs
#include<stdlib.h>
#include <stdio.h>
char s1[10], s2[10];
int a, b;

void drawmenu();
void StartGame();

//Core algorithm
int monkeyking(int m, int n) {
    int num = 0,i;

    //Building dynamic arrays
    int *a = new int[m];
    for (int i = 0; i < m; i++) {
        a[i] = i + 1;
    }
    //Find and delete elements 
    while (true) {
        if (m == 1)
            break;
        for (int i = (num - 1 + n) % m; i < m - 1; i++) {
            a[i] = a[i + 1];
        }
        num = (num - 1 + n) % m;
        m--;
    }

    return a[0];

}

//Input error
void wrong(){
    MOUSEMSG m;
    initgraph(800, 600);
    setbkcolor(WHITE);
    cleardevice();

    IMAGE img;
    loadimage(&img, _T("C:\\Users\\Hanson\\Desktop\\Project1\\timg.jpg"));
    putimage(0, 0, &img);

    setfillcolor(DARKGRAY);
    fillrectangle(200, 200, 600, 250);

    settextstyle(42, 0, "Blackbody");//Format text
    setbkmode(TRANSPARENT);// Remove text background

    outtextxy(210, 205, "Input error, click return");

    settextcolor(BLACK);
    settextstyle(75, 0, "Regular script");
    outtextxy(250, 0, "The monkey is king");

    while (1) {
        m = GetMouseMsg();
        if (m.x >= 200 && m.x <= 600 && m.y > 200 && m.y <= 250) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                StartGame();
            }
        }
    }



}

//result
void OverGame() {
    int c;
    char s[5];

    c = monkeyking(a, b);
    MOUSEMSG m;
    initgraph(800, 600);
    setbkcolor(WHITE);
    cleardevice();

    IMAGE img;
    loadimage(&img, _T("C:\\Users\\Hanson\\Desktop\\Project1\\timg.jpg"));
    putimage(0, 0, &img);


    setfillcolor(DARKGRAY);
    fillrectangle(200, 200, 600, 250);
    fillrectangle(200, 260, 395, 310);
    fillrectangle(405, 260, 600, 310);


    settextstyle(42, 0, "Blackbody");//Format text
    setbkmode(TRANSPARENT);// Remove text background

    itoa(c,s,10);
    outtextxy(230, 205, s);
    outtextxy(250, 205,"Monkey number one is the monkey king");
    outtextxy(250, 265, "sign out");
    outtextxy(455, 265, "return");

    settextcolor(BLACK);
    settextstyle(75, 0, "Regular script");
    outtextxy(250, 0, "The monkey is king");

    while (1) {
        m = GetMouseMsg();
        if (m.x >= 200 && m.x <= 395 && m.y > 260 && m.y <= 310) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                exit(0);
            }
        }

        if (m.x >= 405 && m.x <= 600 && m.y > 260 && m.y <= 310) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                drawmenu();
            }
        }
    }
    getchar();
}

//Start the game
void StartGame()
{
    MOUSEMSG m;
    initgraph(800, 600);
    setbkcolor(WHITE);
    cleardevice();

    IMAGE img;
    loadimage(&img, _T("C:\\Users\\Hanson\\Desktop\\Project1\\timg.jpg"));
    putimage(0, 0, &img);


    setfillcolor(DARKGRAY);
    //           Left 1 high 1 left 2 high 2
    fillrectangle(300,100,500,150);
    fillrectangle(300,200,500,250);
    fillrectangle(350, 300, 450, 350);

    settextstyle(42, 0, "Blackbody");//Format text
    setbkmode(TRANSPARENT);// Remove text background

    outtextxy(315, 105, "Number of monkeys");
    outtextxy(315, 205, "Number of intervals");
    outtextxy(355, 305, "start");

    settextcolor(BLACK);
    settextstyle(75, 0, "Regular script");
    outtextxy(250, 0, "The monkey is king");
    

    while (1) {
        m = GetMouseMsg();
        if (m.x >= 300 && m.x <= 500 && m.y > 100 && m.y <= 150) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                InputBox(s1,10,"Please enter the number of monkeys","Number of monkeys",0);
                a = atoi(s1);
            }
        }

        if (m.x >= 350 && m.x <= 450 && m.y > 200 && m.y <= 250) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                InputBox(s2,10,"Please enter the number of intervals","Number of intervals",0);
                b = atoi(s2);
            }
        }

        if (m.x >= 350 && m.x <= 450 && m.y > 300 && m.y <= 350) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                if (b >= a) {
                    wrong();
                }
                else {
                    OverGame();
                }
            }
        }
    }
    
}

//Initial interface
void drawmenu() {
    MOUSEMSG m;
    initgraph(800, 600);
    setbkcolor(WHITE);
    cleardevice();

    IMAGE img;
    loadimage(&img, _T("C:\\Users\\Hanson\\Desktop\\Project1\\timg.jpg"));
    putimage(0, 0, &img);

    setfillcolor(DARKGRAY);
    fillrectangle(250, 200, 550,250);
    fillrectangle(250, 255, 550,305);

    settextstyle(42, 0, "Blackbody");//Format text
    setbkmode(TRANSPARENT);// Remove text background

    outtextxy(315, 205, "Start the game");
    outtextxy(315, 260, "Exit the game");

    settextcolor(BLACK);
    settextstyle(75, 0, "Regular script");
    outtextxy(250, 0, "The monkey is king");

    while (1) {
        m = GetMouseMsg();
        if (m.x >= 250 && m.x <= 550 && m.y > 200 && m.y <= 250) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                StartGame();
            }
        }

        if (m.x >= 250 && m.x <= 550 && m.y > 255 && m.y <= 305) {
            if (m.uMsg == WM_LBUTTONDOWN) {
                exit(0);
            }
        }
    }
}
int main() {
    drawmenu();
    return 0;
}

 

4, Test results

 

 

 

 

 

 

 

5, Evaluation from 2021

This is the first time in 2017 that I designed and wrote a small program independently after learning a language (I have cooperated to write other programs in the past).

There is no good code format, no good interface, and even the report is not standardized, but it is a project that impressed me so much that I stayed up late to finish it.

For the first time, I created my own visualization program completely from scratch. Although it was terrible, I really enjoyed writing code.

Think, design, write, encounter debugging problems, check data and continue to solve them. I really gained a lot by cycling all the way. It also laid a certain foundation for my future code writing.

Topics: Project