Cycle exercises

Problem Description

  There are m students in the Academy of Peking University Affiliated High School, and they make many decisions democratically every time. According to Robert's rules of procedure, a moderator is needed. The students have a strong sense of democracy and high enthusiasm. They all want to be moderators, of course there is only one moderator. In order to select the host, they thought of a way and thought it was very democratic. The method is:
  everyone form a circle and number each student from 1 to m. Then start counting from 1 and count to n outs. The rest of the students start from the lower position and then start counting from 1. The last thing left is the host. Now that you have numbered the classmates from 1 to m, and agreed to report the number of outs with n, please program and calculate which classmate will become the host.

Input format

  One line, two integers mn separated by spaces.

Output format

  An integer representing the host’s number

Sample input

15 3

Sample output

5

Sample input

200 55

Sample output

93

Data size and convention

  10000>m>0; 100>n>0;
  time limit is 1.0 second

#include <iostream>
using namespace std;

int main()
{
	int m,n;
	while(cin >> m >> n){
		int a[10001];
		for(int i = 1; i <= m; i++){
			a[i] = i;
		}
		int c = 0;
		int count = m;
		while(count > 1){
			for(int i = 1; i <= m; i++){
				if(a[i] != -1){
					c++;
				}
				if(c == n){
					c = 0;
					a[i] = -1;
					count--;
					if(count == 1)
					break;
				}
			}
		}
		for(int i = 1; i <= m; i++){
			if(a[i] != -1){
				cout << a[i] << endl;
			}
		}
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108894646