uva133救濟金髮放(雙向約瑟夫問題)
阿新 • • 發佈:2018-12-26
#include<stdio.h>
int s[25];
int go(int p,int d,int k,int n);
int main()
{
int n;
int k,m;
scanf("%d",&n);
scanf("%d%d",&k,&m);
int left=n;
for(int i=1;i<=n;i++)
{
s[i]=i;
}
int p=n,q=1;
while(left)
{
p=go
q=go(q,-1,m,n);
printf("%3d",p);
left--;
if(p!=q)
{
printf("%3d",q);
left--;
}
s[p]=0,s[q]=0;
if(left) printf(",");
}
printf("\n");
return 0;
}
int go(int p,int d,int k,int n)
{
while(k--)
{
do
{
p=(p+d+n-1)%n+1;
}while(s[p]==0);
}
return p;
}