There are 15 American and 15 Japanese in the plane. However, it cann't takes more than 15 passengers because of the storm. At last they made a decision about that, all of them stand around as a circle and numbering to 7 from 1, the number one 7 have to leave the plane and numbering repeatedly from 1 again. Please design an order which let all of the American could still be safe in the plane.

#include <iostream>
#include 
<string>
using namespace std;

typedef 
struct Linklist
{
    
int m_date;
    Linklist 
*pNext;
}
Node, *List;

void Josephus(Node *head, int start, int interval, int nums)
{
    
if(!head)
        
return;

    Node 
*temp = head;
    Node 
*temp2 = NULL;

    cout
<<"Count From "<<start<<endl;
    cout
<<"interval is "<<interval<<endl;

    
while(start - 1)
    
{
        temp 
= temp->pNext;        
    }

      
    
while(nums)
    
{
        
for(int i = 1; i < interval - 1; i++)
        
{
            temp 
= temp->pNext;
        }


        temp2 
= temp->pNext;
        cout
<<temp2->m_date<<endl;
        temp
->pNext = temp2->pNext;
        delete(temp2);
        temp2 
= NULL; 
        temp 
= temp->pNext;
        nums
--;
    }
   
}

void main()
{
    
int counts = 30;
    Node 
*head = NULL;
    Node 
*temp = NULL;
    
for(int i = 1; i <= counts; i++)
    
{
        Node 
*newNode = new Node;
        newNode
->m_date = i;
        
if(i == 1)
        
{
            head 
= newNode;
            temp 
= head;
        }

        
else
        
{
            temp
->pNext = newNode;
            temp 
= temp->pNext;
        }

    }

    temp
->pNext = head;

    Josephus(head, 
1230);
    cin
>>counts;
}

 

posted on 2009-06-08 17:24  Jackill  阅读(116)  评论(0编辑  收藏  举报