30 个人在一条船上,超载,需要 15 人下船。 于是人们排成一队,排队的位置即为他们的编号。 报数,从 1 开始,数到 9 的人下船。 如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?
这是我的代码但是跑不出来
#include
#include
int count=0;
void digui(int a[]){
int i=1,j,k;
while(a[i]!='\0'){
if(i%9==0){
a[i]=a[i+1];
count++;
k=count;
i++;
printf("%d ",a[i]);
}
if(count%3==0){
digui(a+1);
}
if(k==15)
break;
}
}
int main()
{
int a[30];
int i;
for(i=1;i<=30;i++){
a[i]=i;
}
digui(a+1);
return 0;
}
约瑟夫环
#include <stdio.h>
typedef int Element;
int que[1003], i, head = 0, tail = 0;
void push(Element x) {
que[++tail] = x;
}
void pop() {
++head;
}
Element front() {
return que[head + 1];
}
Element rear() {
return que[tail];
}
int size() {
return tail - head;
}
int main()
{
for(i = 1; i <= 30; ++i)
push(i);
while(size() > 15) {
for(i = 1; i < 9; ++i)
{
push(front());
pop();
}
printf("%d ", front());//按弹出顺序输出
pop();
}
return 0;
}