15个人围成一个圈,报数1.2.3,逢报到3的就退出这圈,问最后一个留下的是几号?
1、用下标做
2、换成针做
3、链表
这题要怎么搞?
请大家帮帮忙!谢谢哈~~~~~~~~
1、下标 #include <stdio.h> #include <stdlib.h>
#define N 15 #define M 3
void main() { int person[N], i = 0,number = 1, count = 0; for(i = 0; i < N; i++) person[i] = i+1; i = 0; while(number < N) { if(person[i] != 0) { count++; if(count == M) { person[i] = 0; count = 0; number++; } } i++; if(i == N) i = 0; } printf("The last person is:"); for(i = 0; i < N; i++) if(person[i] != 0) { printf("%d\n",person[i]); break; } system("pause"); } 2、指针 #include <stdio.h> #include <stdlib.h> #define N 15 #define M 3
void main() { int person[N+1], count = 0, number = 1, i = 0; int * p = person; for(i = 0; i < N; i++) *(p+i) = i+1; *(p+i) = -1; while(number < N) { if(*p != 0) { count ++; if(count == M) { *p = 0; count = 0; number ++; } } p++; if( *p == -1) p = person; } p = person; while(*p != -1) { if(*p != 0) { printf("The last person is:%d\n",*p); break; } p++; } system("pause"); } 3、循环链表 #include <stdio.h> #include <stdlib.h> #define N 15 #define M 3
typedef struct node { int index; struct node * next; } Node, *pNode; void initlist(pNode * list); void output(pNode list); void main() { pNode headlist = NULL; initlist(&headlist); output(headlist); system("pause"); } void initlist(pNode * list) { pNode temp,temp1; int i = 0; temp = (pNode)malloc(sizeof(Node)); if(temp == NULL) { printf("Malloc Failure!\n"); exit(-1); } temp1 = temp; while(i < N) { temp->next = (pNode)malloc(sizeof(Node)); if(temp->next == NULL) { printf("Malloc Failure!\n"); exit(-1); } temp = temp->next; temp->index = i + 1; i++; } temp->next = temp1->next; *list = temp1->next; free(temp1); } void output(pNode list) {
pNode temp1,temp2; temp1 = list; temp2 = temp1; int number = 1,count = 0; while(number < N) { count++; if(count == M) { temp2->next = temp1->next; count = 0; free(temp1); temp1 = temp2->next; number ++; continue; } temp2 = temp1; temp1 = temp1->next; } printf("The last person is:%d\n",temp1->index); free(temp1); }
一句话。上机运行去。 #include<iostream> using namespace std; struct List{ int data; List *next; };
void InitList(List *&l){ l=NULL; }
void OrderInsertList(List *&l,int item){ List *pt=new List; pt->data=item; pt->next=NULL; if(l==NULL||item<l->data){ pt->next=l; l=pt; return; } List *p=l,*q=p->next; while(p){ if(item<p->data)break; q=p; p=p->next; } pt->next=p; q->next=pt; }
void InsertList(List *&l,int item){ List *pt=new List; pt->data=item; /*注意pt的next*/ static List *tail=NULL; if(l==NULL){ l=pt; l->next=l; tail=pt; } else{ pt->next = tail->next; tail ->next = pt; tail=pt; } }
int LenList(List *l){ int i=0; List *p=l->next; while(l!=p){ p=p->next; i++; } return i+1; }
void DeleteList(List *&l,int item){ List *del=l->next; List *before=NULL; while(del != l){ if(del->data==item)break; else { before=del;del=del->next; } } if(l->data ==del->data) { before->next = l->next; l = l->next; del->next = NULL; delete del; } else { before->next = del->next; del ->next =NULL; delete del; } }
void OutputList(List *l){/*注意看这里*/ List *p = l->next; cout<<"打印链表:"<<endl; cout<<l->data<<" "; while(p != l) { cout<<p->data<<" "; p = p->next; } cout<<endl; }
void Order(List *&l,int n){ List *p=l; int x=LenList(p); int i=1; while(1){ if(n==i){ cout<<p->data<<"-->"; DeleteList(p,p->data); x--; i=1; } if(x==1)break; i++; p=p->next; } cout<<"王者: "<<p->data<<endl; }
int main(){ List *a; InitList(a); for(int i=1;i<14;i++) InsertList(a,i); cout<<LenList(a)<<endl; OutputList(a); Order(a,3); return 0; }