2020oop's second 7-3 report (10 points) + excellent physics engine

Input two positive integers n and m ( (1<m<n<=50)), there are n people in a circle, numbered from 1 to n in sequence. Start counting from the first person, and the person who reported m will exit the circle, and the next person will start counting again from 1, and the person who reported m will exit the circle. This cycle continues until the last person is left. Please output the number of people who left the circle in order of exit, and the number of the last person.

Tip: Store the number of each person in the array, start counting from the first person, output the number of the person who reported m, and clear the number to 0, repeat this operation until only one non-zero is left The number is the number of the last person.

Input and output example: description in brackets, no input and output required

Input sample:

5               (n个人报数,n=5)
3               (报数m=3)

Sample output:

No1: 3          (第1个退出圈子的人编号是3)
No2: 1	        (第2个退出圈子的人编号是1)
No3: 5	        (第3个退出圈子的人编号是5)
No4: 2	        (第4个退出圈子的人编号是2)
Last No is: 4   (最后一个人的编号是4)

(array + loop implementation):

#include<iostream>
using namespace std;
int main()
{
    int n,m,i,j,k=0,t,num[60],p=1;
    cin>>n>>m;
    for(i=1;i<=n;i++)
        num[i]=i;
    t=n;
    while(n!=1)
    {
        for(j=1;j<=t;j++)
        {
            if(num[j]!=0) k++;
            if(k==m){
                cout<<"No"<<p<<": "<<j<<endl;
                p++;
                num[j]=0;
                k=0;
                n--;
            }
            
        }
    }
    for(i=1;i<=t;i++)
        if(num[i]!=0)
        cout<<"Last No is: "<<i;
    return 0;
}

Excellent physics engine (same type of topic)

Corolla is addicted to the ark game recently. There are n floating stones forming a circle on the map in the game. With the support of an excellent physics engine, these stones will fall down automatically. She found that the order in which the stones fell was regular. There are n stones in total, counting from the first stone to the mth stone, that is the first falling stone; then counting from 1 after the first falling stone, and counting to The mth stone, that is the second falling stone; and so on. For convenience, these stones are numbered starting from 1. Corolla now wondered which rock was the last to fall?

Input format:

The input contains two integers n and m (1<=m,n<=1000).

Output format:

Output an integer representing the number of the last stone that fell.

Input sample:

10 3

Sample output:

4

Implemented with a linked list

#include<iostream>
using namespace std;
typedef struct p{
    int num;
    struct p *next;
}ss;
int m,n;
void creat()
{
    int i;
    ss* p1,*p2;
    ss *a,*b,*head=NULL;
    b=new ss;
    for(i=0;i<n;i++){
        a=new ss;
            a->num=i+1;
        if(i==0){
            head=a;
            a->next=head;
        }    
        else{
            a->next=head;
            b->next=a;
        }
        b=a;
    }
    p2=b;
    int count=n;
    while(count!=1)
    {
        for(i=0;i<m;i++)
        {
            if(i==m-1)
            {
                p2->next=p1->next;
                count--;
                
            }
            else {
                p2=p2->next;
                p1=p2->next;
            }
        }    
    }
    cout<<p2->num;  
}
int main(){
    cin>>n>>m;
    creat();
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45788060/article/details/108133942