问题:题目:有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的人退出圈子,问最后最后留下的是原来第几号的那位.
#include <stdio.h>
int main()
{ int i,j,k=0,l=0,m=0,A[50];
printf("How many ?\n");
scanf("%d",&i);
for(j=0;j<i;j++)
*(A+j)=j+1;
while(m<i-1)
{if(A[l]!=0) k++;
if(k%3==0)
{A[l]=0;
k=0;
m++;}
l++;
if(l==i) l=0;
}
while(A[l]==0) l++;
printf("%d",A[l]);
return 0;
}
试试这个
#include<stdio.h>
#include<malloc.h>
struct node
{
int num;
struct node *next;
};
struct node *head;
struct node *last;
void cre_list()
{
head = (struct node *)malloc(sizeof(struct node));
last = head;
head->next = head;
}
void display_node()
{
struct node *ptr = head->next;
while(ptr != head)
{
// printf("%d\t",ptr->num);
ptr = ptr->next;
}
//printf("\n");
}
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
ptr->next = head;
last->next = ptr;
last = ptr;
}
void rev_node()
{
struct node *ptr = head;
last->next = head->next;
head = head->next;
free(ptr);
}
void tiren_node()
{
struct node *ptr = head;
struct node *str = ptr->next;
struct node *qtr = str->next;
while(ptr->next != ptr)
{
str = ptr->next;
qtr = str->next;
str->next = qtr->next;
ptr = str->next;
}
printf("%d",ptr->num);
}
int main()
{
int i = 0;
cre_list();
int n;
//printf("please input n:\n");
printf("How many ?\n");
scanf("%d",&n);
// printf("%d\n",n);
for(i = 1; i <= n; i++)
{
add_node(i);
}
display_node();
rev_node();
tiren_node();
return 0;
}
其实题主思路还是不错的,不过其实这道题可能没有想象的那么麻烦
#include <stdio.h>
int x = 3; //报到3退出
int main()
{
int m,n = 0;
scanf("%d", &m);
for (int i = 2; i <= m; ++i)
n = (n+x)%i; //循环取模
printf("%d\n", n+1); //找到后加上除循环外本来那一位 就是原来第几号的那位
return 0;
}
希望对题主有所帮助,可以的话还请点下采纳!