/* 15个人排成一圈,给他们1——15的编号 数到4的人退出,余下的人从下一个位置重新开始报数,直到最后只剩下一个人

问 这个人是几号?
*/
==================数组实现==========================
 
 
    
  1. #include <iostream> 
  2. #include <stdio.h> 
  3. using namespace std; 
  4.  
  5. int list[15]; 
  6.  
  7. void print() 
  8.     cout<<"----------剩下的元素为-------------"<<endl; 
  9. for (int i=0;i<15;i++) 
  10.     cout<<list[i]<<endl; 
  11. void init() 
  12.     for (int i=0;i<15;i++) 
  13.     { 
  14.         list[i]=i+1; 
  15.     } 
  16.  
  17.      
  18.          
  19. void setposition(int *&p)  //这个函数是最难的地方 
  20.     int sum=0; 
  21.     int a=3; 
  22.     while(sum!=a) 
  23.     {    
  24.      
  25.         if (!*p) 
  26.         { //p为0的时候  也 就是说传递进来的是0,所以a=4; 
  27.             a=4; 
  28.             while (!*p)      //找到下一个不为0的点。 
  29.             { 
  30.                 p++; 
  31.                 if (p==list+15) 
  32.                 { 
  33.                     p=list;  
  34.                 } 
  35.                  
  36.             } 
  37.         } 
  38.     else 
  39.     {  //p不为0的时候  
  40.      
  41.         p++; 
  42.         if (p==list+15)   //注意检验p是否为最后一个元素。 
  43.         { 
  44.             p=list;  
  45.         } 
  46.         if (!*p) 
  47.         {  
  48.              
  49.             while (!*p) 
  50.             { 
  51.                 p++; 
  52.                 if (p==list+15) 
  53.                 { 
  54.                     p=list;   
  55.                 } 
  56.                  
  57.             } 
  58.         } 
  59.  
  60.     } 
  61.  
  62.     sum++; 
  63.     } 
  64. int main() 
  65. init(); 
  66. int *p=list; 
  67. for (int count=0;count<14;count++) 
  68.      
  69. setposition(p); 
  70. *p=0; 
  71.  
  72. print(); 
  73. }    
  74. return 0; 
=================链表实现===========================
 
 
   
  1. #include <iostream> 
  2. #include <stdio.h> 
  3. using namespace std; 
  4.  
  5. struct list  
  6.     int data; 
  7.     list * next; 
  8. }; 
  9.  
  10.  list *head=new list; 
  11.  
  12.  list *tail=head; 
  13.  
  14.  void add() 
  15.  { 
  16.         head->data=1; 
  17.      for (int i=2;i<16;i++) 
  18.      { 
  19.          list *p=new list;//记得清零 
  20.          memset(p,0,sizeof(p)); 
  21.          tail->next=p; 
  22.          p->data=i; 
  23.          p->next=head; 
  24.          tail=p; 
  25.      } 
  26.  
  27.  } 
  28.  
  29. void out() 
  30.     cout<<endl<<endl<<endl; 
  31.     cout<<"剩下的人:"<<endl; 
  32.     list *p=head; 
  33. //  printf("head:%d\ttail:%d\t\n",head,tail); 
  34.     while(p!=tail) 
  35.     { 
  36.         cout<<p->data<<endl; 
  37.         p=p->next; 
  38.  
  39.     } 
  40. //      cout<<p->data<<endl; 
  41. //      cout<<"out over"<<endl; 
  42. //          int _a_;cin>>_a_; 
  43. void remove() 
  44.     list *p=head; 
  45.     list *lastp=p; 
  46.  
  47.     for(int count=0;count<14;count++) 
  48.     {    
  49.                 out(); 
  50.             p=p->next; 
  51.             for (int i=0;i<2;i++) 
  52.             {    
  53.                 lastp=lastp->next; 
  54.                 p=p->next; 
  55.             } 
  56.  
  57.     //  cout<<"free "<<p->data<<endl; 
  58.         lastp->next=p->next; 
  59.         if (p==head)  //here; 
  60.         { 
  61.             head=lastp->next; 
  62.         } 
  63.         if (p==tail) 
  64.         { 
  65.             tail=lastp; 
  66.         } 
  67.         free(p); 
  68.         p=lastp->next; 
  69.         lastp=p; 
  70.          
  71.     } 
  72.  
  73. int main() 
  74. add(); 
  75. remove(); 
  76. return 0; 
 
=========================================
感想:本来用链表完了以后觉得挺麻烦的,这问题用数组解决应该很容易,一实践才发现根本不是那回事,数组需要考虑的非常周密,编写的过程中犯了很多错误,只好一点一点修改解决。
现在快累死了,从7点到十点。解决了这个问题,总的来说 还是有一点收获的。
2011年12月26日 21:57:43