King's Game HDU - 5643

題目鏈接
In order to remember history, King plans to play losephus problem in the parade gap.He calls n(1≤n≤5000) soldiers, counterclockwise in a circle, in label 1,2,3…n.

The first round, the first person with label 1 counts off, and the man who report number 1 is out.

The second round, the next person of the person who is out in the last round counts off, and the man who report number 2 is out.

The third round, the next person of the person who is out in the last round counts off, and the person who report number 3 is out.

The N - 1 round, the next person of the person who is out in the last round counts off, and the person who report number n−1 is out.

And the last man is survivor. Do you know the label of the survivor?
Input
The first line contains a number T(0<T≤5000), the number of the testcases.

For each test case, there are only one line, containing one integer n, representing the number of players.
Output
Output exactly T lines. For each test case, print the label of the survivor.
Sample Input
2
2
3
Sample Output
2
2

Hint:
For test case #1:the man who report number 11 is the man with label 11, so the man with label 22 is survivor.

For test case #1:the man who report number 11 is the man with label 11, so the man with label 1 is out. Again the the man with label 2 counts 11, the man with label 33 counts 22, so the man who report number 22 is the man with label 33. At last the man with label 22 is survivor.

首先什麼是約瑟夫環?
n個人站成一圈, 編號爲1-n, 開始報數, 報到q的淘汰, 求最後的贏家;
對於n, 與 q, ans=(ans+q)%i;q表示報數爲q的淘汰, i表示本局有多少人(n), ans表示上局獲勝的人的下標(下標由0-(n-1)編號);
而此題中q是變化的, 需要稍微變形;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    int n;
    int a[105][105];
    while(cin>>n&&n!=EOF)
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i++)
        {
            int x,y;
            cin>>x>>y;
            a[x][y]=1;
        }
        int n1=0,n2=0;
        for(int i=0;i<=100;i++)
        {
            for(int j=0;j<=100;j++)
            {
                if(a[i][j]==1)
                {
                    n1++;
                    break;
                }
            }
        }
        for(int i=0;i<=100;i++)
        {
            for(int j=0;j<=100;j++)
            {
                if(a[j][i]==1)
                {
                    n2++;
                    break;
                }
            }
        }
        int s=min(n1,n2);
        cout<<s<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章