There are n people in a circle, the order of automatic arranging, from the first person to start off (from the 1~3 count), where 3 of the people who reported out of the circle, asked the last person left in the first number.

Source: Internet
Author: User
         
* * Copyright (c) 2012, Computer College of Yantai University         
* All rights reserved.         
* Author:  Liu Tongbin       
* Date of Completion: December 05, 2012         
* Version number: v1.0  
* *               
Input Description:    
* Problem Description: There are n people in a circle, the order of automatic arranging, starting from the first person to count (from the 1~3 numbered), c9/>* who           report 3 exit Circle, ask the last person left in the first date.
* Program output:
* Problem Analysis: slightly        
* Algorithm design: Slightly * *

#include <iostream>

using namespace std;

int main ()
{
	int num[50];

	int i,j,k,m,n;

	int *p;

	cout<<endl<< "Please enter the total number:" <<endl;

	cin>>n;

	P=num;

	for (i=0;i<n;i++)
	{
		* (p+i) =i+1;   In order of 1 to n, each person is numbered
	}

	i=0;    I for each cycle of the count variable

	k=0;    K is the count variable m=0 according to 1 2 3 off

	;    M for the number of exits

	while (m<n-1)  //When the number of exits is less than n-1 (that is, the number of exits is greater than 1 o'clock) Execute loop body
	{
		if (* (p+i)!=0)
		{
			k++;
		}

		if (k==3)    //The exit number is set to 0
		{
			* (p+i) =0;

			k=0;

			m++;
		}

		i++;

		if (i==n)
		{
			i=0;//count to tail I revert to 0
		}
	} while

	(*p==0)
	{
		p++;
	}

	cout<< "The last one is the" <<*p<< "number. "<<endl;

	return 0;
}

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.