69,371
社区成员
发帖
与我相关
我的任务
分享
int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int call;
int number;
int i=0;
number=16;//出队次数
call=1;//出队循环变量
while(number>0)
{
if((circle[i]!=-1))//当前元素未出队
{
if((call%3) == 0)//当前到循环点
{
circle[i]=-1;
call++;
number--;
}
else
{
call++;
}
}
i++;
if(i>=17)
i%=17;
}
for(i=0;i<17;i++)
{
if(circle[i]!=-1)
{
printf("the num is %d\n",i);
}
}
system("pause");
更改了你的求解逻辑printf("the num is %d",i);
这个后面最后加个'\n'
printf("the num is %d\n",i);
这样结果很清晰for(i=1;i<=17;i++)
这个错误,应该是
for(i=0;i<=16;i++)
程序里面连个for loop都有这个问题
while(circle[i]!=-1)
应该是
if(circle[i]!=-1)
}else if(call%3=0){
语法错误,应该是
else if(call%3==0)
#include"stdio.h"
#include"stdlib.h"
struct node
{
int num;
struct node *next;
};
struct node *head = NULL;
struct node *last = NULL;
void create(int n)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
head = p;
p->num = n;
last = p;
last->next = head;
}
else
{
last->next = p;
p->num = n;
last = p;
last->next = head;
}
// printf("%d %d\n",p->num,head->num);
}
void my_move(struct node *p)
{
struct node *q,*front;
q = head;
front = head;
if(p == head)
{
while(q->next != head)
{
q=q->next;
}
front = p;
q->next = p->next;
head = p->next;
free(front);
}
else
{
while(q!=p)
{
front =q;
q=q->next;
}
if(q->next ==head)
{
front->next = head;
free(q);
}
else
{
front->next = q->next;
free(q);
}
}
}
int main()
{
int n;
int i;
printf("please input the num\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
create(i);
}
printf("create success\n");
struct node *p = head;
while(n!=1)
{
struct node *q;
p = p->next->next;
q = p;
p = p->next;
my_move(q);
n--;
}
printf("the end staty is%d\n",p->num);
return 0;
}
//假设有n个人团团围做,从第1个人开始数数,数到第m个人时候,第m个人出列,
//然后继续从1开始数数,数到第m个人退出
#include <stdio.h>
#include <conio.h>
int i,k,t;
int n,m;
static char f[1001];//0该座位未出圈,1该座位已出圈
void main() {
while (1) {
printf("Input n m(1000>=n>=m>=1):");
fflush(stdout);
rewind(stdin);
if (2==scanf("%d%d",&n,&m)) {
if (1000>=n && n>=m && m>=1) break;
}
}
t=0;//已出圈总人数
i=1;//座位编号
k=1;//当前要数的数
while (1) {
if (0==f[i]) {
if (m==k) {
t++;
f[i]=1;
printf("%3d ",i);
if (0==t%10) printf("\n");
if (t>=n) break;
}
k++;if (k>m) k=1;
}
i++;if (i>n) i=1;
}
cprintf("Press any key ...");
getch();
}
#include<stdio.h>
int main()
{
int circle[17]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int call;
int number;
int i;
number=16;
call=1;
while(number>0)
{
for(i=0;i<=16;i++){
if(circle[i] != -1) {
if(call%3 == 0) {
circle[i] = -1;
number--;
}
call ++;
}
}
}
for(i=0;i<=16;i++){
if(circle[i]!=-1){
printf("the num is %d\n",i);
}
}
//system("pause");
return 0;
}