8
研研研研研研研研研研研研研研研研 研研 1. 研研研研研研研研 N研研研 一, 1,2,3……N,研研研研 研研研研研研研研研 研研研研研研研研 一()。一 m 1 研研研研研研研研研研研 1,2,3……m,研 m 研 研 研 研 研 ,,一,一 研研研 研研研研研研 ,。

Solving Joseph Problem with Linked List

  • Upload
    lex

  • View
    221

  • Download
    7

Embed Size (px)

DESCRIPTION

data structureC programming language

Citation preview

1. N1,2,3N,!"#$%&'()*+,-./$m01 1/234)1,2,3m,4m56789#$0:56;?@1/AB?C4)DEF?GH+2. I$JKLMN8OPQR(STUVI$W?X#include #include #include #include #define M 4000struct Lnode{int num; //numberdouble pw; //#$passwordstruct Lnode *next;};void deletef(struct Lnode *p,struct Lnode *head){struct Lnode *q;q=head;while(q->next!=p)q=q->next;if(p->next!=NULL)q->next=p->next;else{q->next=NULL;}free(p);}//YZ[\]^void main(){int i,j;double x,m;clock_t start,end;struct Lnode *head,*p,*rear,*f;head=(struct Lnode*)malloc(sizeof(struct Lnode));rear=head;for(i=0;ipw=(double)(1+random(1000));p->num=i+1;rear->next=p;rear=p;}rear->next=NULL;//_`abcm=(double)(1+random(1000));p=head->next;start=clock();for(i=0;inext;else{p=p->next;}}printf("%d\t",p->num);m=p->pw;if(p->next!=NULL)f=p->next;else{f=head->next;}deletef(p,head);p=f;}else if(x==1){printf("%d\t",p->num);m=p->pw;if(p->next!=NULL)f=p->next;else{f=head->next;}deletef(p,head);p=f;}else{if(xnext;else{p=p->next;}}printf("%d\t",p->num);m=p->pw;if(p->next!=NULL)f=p->next;else{f=head->next;}deletef(p,head);p=f;}}printf("%d\n",p->num); //noSTH;end=clock();printf("time spent: %f",(end-start)/CLK_TCK);}3. I$pqrsI$tuvwQRxy"z{|})n"~i8O(n^2),xy"pO(1)+4. JVMicrosoft Visual Studio rB})05 1/81 20 B5W?X4! 10 12 14 1 1! 20 22020400!0100120140101!0200f(") # 0.5"^2 $ 4.3!" % 12.35numbernum&'r(ol)nomial (num&'r)9I)@I@+LM:VO(n^2)+/)W?X5. t:V?=a-+n 01 4)CE(n$1)LM?rC56;m(m%1)1 (m%2)2 23i(n$1)+(n$1)4)H;"n H;("%*)+nxbc-+(n)H;(n$1)H;(n$2)H;1 +I$5Qn irs-N+}NO(n)+Time/s Number/5.708 59.349 614.044 719.329 825.351 934.226 1046.442 1152.805 1264.643 1377.741 1490.095 15107.792 16125.136 17148.537 18163.79 19184.27 20