2020牛客国庆集训派对day2 AKU NEGARAKU
來源:牛客網:
題目描述
1st Academy is an international leadership training academy based in Kuala Lumpur. Every year, the company trains thousands of people to be supplied to companies around the world. To be fair amongst all the trainees, the company will do the selection process using numbering system. The trainees will choose a number from 1 to N, and one number is not limited to only one trainee. The N represents the total number of companies that request trainees from the academy. A number, M, would be picked at random, and the selection starts with a trainee whose number is 1, and then in every M’th people after that, wrapping around to 1 after N, and ignoring trainees already selected. For example, if N = 15 and M = 6, trainees would be selected in the order: 6, 12, 3, 10, 2, 11, 5, 15, 13, 9, 14, 4, 1, 8, 7. All the selected trainees except the last one (which is number 7) will be supplied to companies outside of Malaysia.
However, Leong preferred to stay and work in Malaysia. To him, there is no better place other than Malaysia. He does not mind being located anywhere as long it is Malaysia. As a friend of him, you could help him to choose a number that will save him from being located outside.
Input
輸入描述:
Input will consist of a series of lines, each line containing the number of companies (N) with 1 \leq N \leq 15001≤N≤1500, and a skipping value (M) with 1 \leq M \leq 501≤M≤50. The values will be terminated by a line consisting of double zeros (0 0) as shown in sample input output.
輸出描述:
Output will consist of a series of lines, one for each line of the input. Each line will consist of the number M according to the above scheme.
示例1
輸入
復制
輸出
復制
題意:
已知n個人(以編號1,2,3…n分別表示)圍坐在一張圓桌周圍。從編號為k的人開始報數,數到m的那個人出列;他的下一個人又從1開始報數,數到m的那個人又出列;依此規律重復下去,直到圓桌周圍的人全部出列。問最后一個出列的人是多少號
題解:
經典約瑟夫環問題
我們看一下樣例:
15 6
依次出列的是:
6, 12, 3, 10, 2, 11, 5, 15, 13, 9, 14, 4, 1, 8, 7.
最后是7,7就是我們的答案
這個題麻煩在已經出列的人不能再被算在里面
第一次6出列后,再往后數數時就不能再算6,那該怎么做?
我們可以這樣,當指針指向第一個要出列的數6時,6出列后,
原本是:
1 2 3 4 5 6 7 8 9 …
更改為:
1 2 3 4 5 7 8 9…
讓6后面的每個數向前覆蓋,然后總量也從15變為14,(第15位變成0)
指針也向前移動,因為6已經不存在了
一直模擬這個過程即可,當只剩下兩個數,且存在有一個數已經被移開,那最后吃雞的數就是我們要的
代碼:
#include<bits/stdc++.h> using namespace std; const int maxn=2000; int pos[maxn]; int main() {int m,n;while(cin>>m>>n)//總人數 以n循環{if(m==0||n==0)break;for(int i=1;i<=m;i++)pos[i]=i;//給每個數一個編號int temp=0;while(true){temp+=n;//temp%=m;//if(temp==0)temp=m;//指向最后一個人 for(int i=temp;i<m;i++)pos[i]=pos[i+1];//第temp的數不存在,編號前移pos[m]=0;//去掉一個人 m--; //人數少一temp--;if(pos[2]==0)break;//當倒數第二個數刪除以后 }cout<<pos[1]<<endl;} return 0; }總結
以上是生活随笔為你收集整理的2020牛客国庆集训派对day2 AKU NEGARAKU的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ikey2032的安装和使用方法
- 下一篇: 2020牛客国庆集训派对day2 MAT