多个人围成一圈,顺序排号,从第一个人开始循环报数1,2,3,凡是报到3的人退出圈子,问最后剩下的是原来的第几号的那个。 人数最少为4人。 注意!这个是C++版本的,请用支持C++的编译器来编译。比如VC6/VS系列。 #include <stdio.h> #include <conio.h> #include <vector> #include <string> #include <windows.h> using namespace std; void Delay( double timePlus ); void Color( string stateIn ); void InitDatas( vector<unsigned int> *arrIn, unsigned int num ); void OutputArrState( vector<unsigned int> arrIn ); void Game( vector<unsigned int> *arrIn ); int main( void ) { vector<unsigned int> arr; unsigned int num = 0; bool State_inputAgain = false; Color( "INIT" ); //1.输入数据 Color( "INPUT" ); do { printf( "请输入参与游戏的人数(建议不要超过10,不然运行时间会很长):" ); scanf_s( "%u", &num ); if( num <= 3 ) { Color( "INPUTERROR" ); printf( "至少要有4个玩家参加游戏。请重新输入。/r/n" ); State_inputAgain = true; } else { State_inputAgain = false; } } while ( State_inputAgain ); //2.初始化数据 InitDatas( &arr, num ); //3.开始玩游戏 Game( &arr ); Color( "INIT" ); printf_s( "/r/n/r/nEnd.按任意键退出。/r/n"); _getch(); return 0; } void Game( vector<unsigned int> *arrIn ) { unsigned int i = 0; unsigned int arrPos = 0; unsigned int sCounter = 1; unsigned int counter = 0; Color( "RUN" ); printf_s( "/r/n游戏开始。/r/n" ); Delay( 2 ); OutputArrState( *arrIn ); do { for( i = 1; i <= 3; i++ ) { counter++; if( i != 3 ) { printf_s( "[%u]第%u轮:%u号玩家说,我是%u。/r/n", counter, sCounter, arrIn->at(arrPos), i ); Delay( 2 ); arrPos++; } else { Color( "RUNTIP" ); printf_s( "[%u]第%u轮:%u号玩家说,我是%u,我退出。/r/n", counter, sCounter, arrIn->at(arrPos), i ); Delay( 4 ); Color( "RUN" ); arrIn->erase( arrIn->begin() + arrPos ); sCounter++; OutputArrState( *arrIn ); Delay( 3 ); } if( arrIn->size() == 1 ) { counter++; Color( "END" ); printf_s( "[%u]第%u轮:现在只剩玩家%u了,游戏结束。/r/n", counter, sCounter, arrIn->at(0) ); Delay( 3 ); break; } if( arrPos == arrIn->size() ) { arrPos = 0; } } }while( arrIn->size() > 1 ); } void InitDatas( vector<unsigned int> *arrIn, unsigned int num ) { unsigned int i = 1; unsigned int length = num; for( i = 1; i <= length; i++ ) { arrIn->push_back( i ); } } void OutputArrState( vector<unsigned int> arrIn ) { unsigned int i = 0; unsigned int length = arrIn.size(); if( arrIn.size() == 0 ) { return; } printf_s( "/r/n当前玩游戏的人有:/r/n" ); Delay( 1 ); for( i = 0; i < length; i++ ) { printf_s( "%u.玩家%u。/r/n", (i+1), arrIn.at(i) ); Delay( 0.5 ); } printf_s( "/r/n" ); } void Delay( double timePlus ) { DWORD sleepTime = (DWORD)(500 * timePlus); Sleep( sleepTime ); } void Color( string stateIn ) { if( stateIn == "INIT" ) { system( "COLOR 07" ); } else if( stateIn == "INPUT" ) { system( "COLOR 1A" ); } else if( stateIn == "INPUTERROR" ) { system( "COLOR 47" ); } else if( stateIn == "RUN" ) { system( "COLOR 0E" ); } else if( stateIn == "RUNTIP" ) { system( "COLOR 4E" ); } else if( stateIn == "END" ) { system( "COLOR 0C"); } }