Joseph Ring Question

Source: Internet
Author: User

1 /*2 the question of Joseph's ring is actually a circular list problem.3 */ 4#include <iostream>5#include <ctime>6 using namespacestd;7 8 //The idea of dynamic programming is somewhat similar to the idea of solving the main element problem.9 //clever use of 0,1 arrays instead of the deletion of linked list nodesTen intSUN1 (Const intNConst intC) One { A     intI=0, j=0, n=n,s=0; -     intt=0; -     int*a=New int[N]; the      for(i=0; i<n;i++) -     { -a[i]=1; -     } +      -      while(0!=N) +     { AS+=A[J];//the preceding numbers add up to a number of C out of each at         if(c==s) -         { -a[j]=0; -s=0;//to clear 0 ---N; -             if(0!=N) incout<<j+1<<" -";//S[j] Actually corresponds to the first j+1 personal -             Else to             { +cout<<j+1<<Endl; -t=j+1; the             } *                  $         }Panax Notoginseng         //j= (j+1)%N;//circular queue, handled very cleverly here -J= (j==n-1)?0: j+1; the     } +     Delete[]a; A     returnT; the      + } - /* $ a recursive approach $ n=1,winner=0; - n=i,winner= (Winner (0) +c)%i; - winner +=1; the */ - intSun2 (Const intNConst intC)Wuyi { the     intf=0; -      for(intI=2; i<=n;i++) Wu     { -f= (f+c)%i; About     } $     returnf+1; - } - /*Circular link List*/  - intSUN3 (Const intNConst intC) A { +     structnode the     { -         intnum; $Node *Next; theNodeConst inti) the         { thenum=i; thenext=NULL; -         } in     }; the     //Initialize the list of links theNode *first=NULL; AboutNode *opp=First ; the      for(intI=1; i<=n;i++) the     { theNode *t=Newnode (i); +         if(first==NULL) -first=T; the         ElseBayiopp->next=T; theopp=T; the     } -      -opp->next=First ; the     intWinner=0; theopp=First ; the      while(true) the     {     -          for(intI=2; i<=c-1; i++)//notice here that the parent node of the dequeue node is found theOpp=opp->Next; theNode *temp=opp->Next; theCout<<temp->num;94         if(opp!=temp) the         { thecout<<" -"; the             //Delete a node98Opp->next=temp->Next; AboutOpp=opp->Next; -             Deletetemp;101         }102         Else103         {104Winner=temp->num; thecout<<"\ n"; 106             Deleteopp;107              Break;108         }109     } the     returnwinner;111          the }113      the      the  the intMain ()117 {118Ios::sync_with_stdio (false);119     intn=0, c=0; -cout<<"Please enter the number of people:n=";121Cin>>N;122cout<<"Please enter:c=";123Cin>>C;124cout<<"winner is"&LT;&LT;SUN1 (n,c) <<Endl; theclock_t end1=clock ();126cout<<"winner is"<<sun2 (n,c) <<Endl;127clock_t end2=clock (); -cout<< (END2-END1)/float(CLOCKS_PER_SEC) <<"seconds"<<Endl;129cout<<"winner is"<<sun3 (n,c) <<Endl; the     return 0;131}

Joseph Ring Question

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.