There are n people in a circle, sequential numbering, from the first person began to count, where the report 3 of the person out of the circle, to write procedures, ask the last left is the original number of the first?

Source: Internet
Author: User

1, there are n people around in a circle, sequential numbering, from the first person began to count, where 3 of the people out of the circle, write procedures, ask the last left is the original number of the person.

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int date;
struct Node *next;
}node,*linklist;
linklist createlist (int n) {
int i;
Linklist head,pre,p;
Head=pre= (linklist) malloc (sizeof (Node));
head->date=1;
for (i=2;i<=n;i++) {
p= (linklist) malloc (sizeof (Node));
p->date=i;
pre->next=p;
Pre=p;
}
pre->next=head;
return head;
}
void Out_circle (linklist head) {
int n;
Linklist pre,p;
P=head; N=1;
while (p->next!=p) {
Pre=p; p=p->next;
n++;
if (n==3) {
pre->next=p->next;
Free (p);
p=pre->next;
N=1;
}
}
printf ("Last left is%d bit: \ n", p->date);
}
int main () {
int n;
Linklist head;
printf ("Input number: \ n");
scanf ("%d", &n);
Head=createlist (n);
Out_circle (head);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.