约瑟夫环动画演示

#define _CRT_SECURE_NO_WARNINGS
#include<easyx.h>            // 引用图形库头文件
#include<iostream>
#include<string>
#include<graphics.h>      
#include<conio.h>
#include<math.h>
#define PI 3.1415926
using namespace std;

char str[10000];

int R = 250;
int R1 = 320;

typedef struct node
{
    int num;
    int x;
    int y;
    int r;
    node *next;
}node, *link;

link create_link(int n);
void draw_circle(node* head, int n, int m);

int main()
{
    int n, m;
    cout << "Joseph Ring Game Start!!!" << endl;
    cout << "请输入总人数n和报数值m:" << endl;
    cin >> n >> m;
    initgraph(720, 720);//初始化界面
    setorigin(360, 360);//设置原点
    setfillcolor(BLUE);//设置填充颜色
    settextcolor(WHITE);//设置字体颜色
    draw_circle(create_link(n), n, m);//画环
    return 0;
}

node* create_link(int n)//建链表
{
    node *p, *q;
    p = (node*)malloc(sizeof(node));
    p->next = NULL;
    p->next = p;
    p->r = 50;
    for (int i = 1; i <= n; i++)
    {
        q = (node*)malloc(sizeof(node));
        q->num = i;
        q->r = 50;
        q->x = cos(360.0 / n * (i - 1) / 180 * PI)*R;
        q->y = -1 * sin(360.0 / n * (i - 1) / 180 * PI)*R;
        q->next = p->next;
        p->next = q;
    }
    return p;
}

void print_index(int n)//打印圆编号
{
    node *p1, *q1;
    p1 = (node*)malloc(sizeof(node));
    p1->next = NULL;
    p1->next = p1;
    for (int i = 1; i <= n; i++)
    {
        q1 = (node*)malloc(sizeof(node));
        q1->num = i;
        q1->x = cos(360.0 / n * (i - 1) / 180 * PI)*R1;
        q1->y = -1 * sin(360.0 / n * (i - 1) / 180 * PI)*R1;
        q1->next = p1->next;
        p1->next = q1;
    }
    p1 = p1->next;
    for (int i = 0; i < n; i++)
    {
        TCHAR b[100];
        _stprintf(b, _T("%d"), p1->num);
        outtextxy(p1->x, p1->y, b);
        p1 = p1->next;
    }
}

void draw_circle(node* z, int n, int m)//画环
{
    node*p = z;
    int j = 0;

    while (p->next != z)
    {
        j++;
        p = p->next;
    }

    //删除头结点
    node *q = p->next;
    p->next = q->next;
    free(q);

    while (1)
    {
        for (int i = 0; i < j; i++)//打印圆
        {
            fillcircle(p->x, p->y, p->r);
            p = p->next;
        }

        print_index(n);//打印圆编号

        for (int i = 0; i < m - 1; i++) //移动到删除节点位置        
        {
            p = p->next;

            setfillcolor(RED);
            fillcircle(p->x, p->y, p->r);
            Sleep(500);
            setfillcolor(BLUE);
            fillcircle(p->x, p->y, p->r);
        }
        if (j == 1)//打印结果
        {
            TCHAR s[] = _T("最后一个圆的位置是");
            TCHAR a[100];
            outtextxy(-60, 0, s);
            _stprintf(a, _T("%d"), p->num);
            outtextxy(0, 20, a);
            cout << endl;
            _getch();
            break;
        }

        //删除节点
        node *q = p->next;
        p->next = q->next;
        free(q);
        j--;

        Sleep(600);
        getbkcolor();
        cleardevice();
    }
}

 

#define _CRT_SECURE_NO_WARNINGS
#include<easyx.h>            // 引用图形库头文件
#include<iostream>
#include<string>
#include<graphics.h>      
#include<conio.h>
#include<math.h>
#define PI 3.1415926
using namespace std;

char str[10000];

int R = 250;
int R1 = 320;

typedef struct node
{
    int num;
    int x;
    int y;
    int r;
    node *next;
}node, *link;

link create_link(int n);
void draw_circle(node* head, int n, int m);

int main()
{
    int n, m;
    cout << "Joseph Ring Game Start!!!" << endl;
    cout << "请输入总人数n和报数值m:" << endl;
    cin >> n >> m;
    initgraph(720, 720);//初始化界面
    setorigin(360, 360);//设置原点
    setfillcolor(BLUE);//设置填充颜色
    settextcolor(WHITE);//设置字体颜色
    draw_circle(create_link(n), n, m);//画环
    return 0;
}

node* create_link(int n)//建链表
{
    node *p, *q;
    p = (node*)malloc(sizeof(node));
    p->next = NULL;
    p->next = p;
    p->r = 50;
    for (int i = 1; i <= n; i++)
    {
        q = (node*)malloc(sizeof(node));
        q->num = i;
        q->r = 50;
        q->x = cos(360.0 / n * (i - 1) / 180 * PI)*R;
        q->y = -1 * sin(360.0 / n * (i - 1) / 180 * PI)*R;
        q->next = p->next;
        p->next = q;
    }
    return p;
}

void print_index(int n)//打印圆编号
{
    node *p1, *q1;
    p1 = (node*)malloc(sizeof(node));
    p1->next = NULL;
    p1->next = p1;
    for (int i = 1; i <= n; i++)
    {
        q1 = (node*)malloc(sizeof(node));
        q1->num = i;
        q1->x = cos(360.0 / n * (i - 1) / 180 * PI)*R1;
        q1->y = -1 * sin(360.0 / n * (i - 1) / 180 * PI)*R1;
        q1->next = p1->next;
        p1->next = q1;
    }
    p1 = p1->next;
    for (int i = 0; i < n; i++)
    {
        TCHAR b[100];
        _stprintf(b, _T("%d"), p1->num);
        outtextxy(p1->x, p1->y, b);
        p1 = p1->next;
    }
}

void draw_circle(node* z, int n, int m)//画环
{
    node*p = z;
    int j = 0;

    while (p->next != z)
    {
        j++;
        p = p->next;
    }

    //删除头结点
    node *q = p->next;
    p->next = q->next;
    free(q);

    while (1)
    {
        for (int i = 0; i < j; i++)//打印圆
        {
            fillcircle(p->x, p->y, p->r);
            p = p->next;
        }

        print_index(n);//打印圆编号

        for (int i = 0; i < m - 1; i++) //移动到删除节点位置        
        {
            p = p->next;

            setfillcolor(RED);
            fillcircle(p->x, p->y, p->r);
            Sleep(500);
            setfillcolor(BLUE);
            fillcircle(p->x, p->y, p->r);
        }
        if (j == 1)//打印结果
        {
            TCHAR s[] = _T("幸存者是");
            TCHAR a[100];
            outtextxy(-27, 0, s);
            _stprintf(a, _T("%d"), p->num);
            outtextxy(0, 20, a);
            cout << endl;
            _getch();
            break;
        }

        //删除节点
        //setfillcolor(RED);
        /*q->r*=1.5;
        while (q->r>q->r*0.1) {
            q->r*0.8;
        }*/

        node *q = p->next;
        p->next = q->next;
        free(q);
        j--;

        Sleep(600);
        getbkcolor();
        cleardevice();
    }
}

 

posted @ 2020-10-22 08:52  XXXSANS  阅读(447)  评论(0编辑  收藏  举报