Rank in a circle 3 and exit the last person location-exercise 8.5 in C Language tan haoqiqiang

Source: Internet
Author: User

Rank in a circle 3 and exit the last person location-exercise 8.5 in C Language tan haoqiqiang
There are n people in a circle, ordered by number. When the first person reports data (from 1 to 3), the person who reports data to the third party quits the circle, the last question is the original location /***************************** * ************************ E8.5 Plan: you can call 1 to 3 in a loop, instead of "CREATE -------------------------------- By: Idooi LiuTime: 2015/10/15-1500 ----------------------------------************************************ ******************/

#include <stdio.h>#include <stdlib.h>int expThree(int n, int manArray[]);int main(void){    int nMan;       //How many people    char *pMan;     //The array    int siteLast;   //The last person's site    int i;    printf("Please input how many people:\n");    scanf("%d", &nMan);    pMan=malloc(nMan*sizeof(char));    //Assignment    for(i=0; i<nMan; i++)        *(pMan+i)='Y';    siteLast=expThree(nMan, pMan);    printf("The last one\'s site is %d\n", siteLast);    free(pMan);    return 0;}int expThree(int n, int manArray[]){    int i, j;    int siteLast;   //The last person's site    int flag=0;     //Count off    int k;    for(i=0; i<n; i++)    {        k=0;        //For check how many people last        for(j=0; j<n; j++)        {            if(manArray[j]=='Y')                k++;            siteLast=j;        }        if(k==1)        //Only one person            break;        if(manArray[i]=='Y')        {            flag++;     //The person who in the team take the next number            //If the person's number is divisible by 3, take out of the team            if(flag%3==0)                manArray[i]='N';        }    }    return siteLast;}

 


Related Article

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.