HOJ 1017 Joseff's problem II

"조세 프 의 문제" 제목 설명:
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from amongn people, numbered 1, 2, ..., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the firstk are good guys and the last k bad guys. You have to determine such minimalm that all the bad guys will be executed before the first good guy.
Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 Output
The output file will consist of separate lines containing m corresponding tok in the input file.
Sample Input
3
4
0

Sample Output
5
30
    :   :          2*k  ,     0 1                 ,                  ,                            ;
           :                   ;                            ,     ,     0,        ;
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 30

using namespace std;
typedef struct Seqlist
{
    int guys[MAX];
    int n;
}*PSeqlist;

PSeqlist creatNulllist()//       ;
{
    PSeqlist plist;
    plist = (PSeqlist)malloc(sizeof(struct Seqlist));
    if(plist != NULL)
    {
        plist->n = 0;
    }
    else printf("out of speace!
"); return plist; } void Findguys(PSeqlist plist,int n)// , { int m,i,j,k; for(m = 1;;m++)// , m ; { k = n; i = 0; while(k > 0) { if((i + m) < k)// m ; j = i + m - 1; else j = (i + m - 1)%k; if(k > n/2) { if(plist->guys[j] == 1) break;// , , } for(i = j;i < k;i++) { plist->guys[i] = plist->guys[i + 1];// , } k--; i = j; } if(k <= 0)// , , { printf("%d
",m); return; } } } int main() { int num[MAX]; int i,j,n = 0; PSeqlist plist; for(i = 0;;i++)// { scanf(" %d",&num[i]); if(num[i] == 0) break; n++; } for(i = 0;i < n;i++) { plist = creatNulllist(); for(j = 0;j < 2*num[i];j++) { if(j < num[i]) plist->guys[j] = 1;// else plist->guys[j] = 0;// } Findguys(plist,2*num[i]); } return 0; }

좋은 웹페이지 즐겨찾기