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
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.