⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithm.cpp

📁 算法设计狼吃兔子问题的c语言实现
💻 CPP
字号:
// algorithm.cpp : Defines the entry point for the console application.
//
#include "iostream"
using namespace std;

void wolf_find_rabbit();
void Joseph_loop();
void Match();

int main(int argc, char* argv[])
{
	int choice;
	cout<<"\n\t\t Algorithm simulation"<<endl;

	cout<<"\n 1: wolf_find_rabbit   2: Joseph loop   3: Match    0: Exit"<<endl;

	cout<<"Please select a number to continue:";
	cin>>choice;

	while(1){
	
		switch(choice){
		case 1:
			wolf_find_rabbit();
			break;
		case 2:
			Joseph_loop();
			break;
		case 3:
			Match();
			break;
		default:
			return;
		}
		cout<<"\n 1: wolf_find_rabbit   2: Joseph loop   3: Match    0: Exit"<<endl;

		cout<<"Please select a number to continue:";
		cin>>choice;
	}
}

void wolf_find_rabbit(){

	int array[11];
	int i,j;
	for(i=1;i<=11;i++){
		array[i]=0;
		//cout<<"  "<<array[i];
	}
	cout<<endl;
	int n=0;//要查找的洞的下标
	for(j=0;j<10;j++)
	{
		n+=j;
		int m = n%10;
		//cout <<"m= "<<m<<"  ";
		array[m]=1;//查找过的洞标记为1
	}
	cout<<endl<<"After 1000 times,those holes have not been found:"<<endl;
	for(i=0;i<10;i++)
	{
		if(array[i]==0)
		{
			cout<<"The "<<i<<" hole has not been found!"<<endl;
		}
	}
	return;
}


void Joseph_loop(){
	int m,n,step=0,index=0,cir=0;
	cout<<"\tJoseph_loop\n";
	cout<<"Please input the total nodes of the loop:";
	cin>>m;
	cout<<"Please input the step:";
	cin>>n;

	char *out=(char*)malloc(m);   
	if(!out){
		return;   
	}
		
	memset(out,0,m);   
	while(1)   
	{   
		if(step==m)  break;   
		while(cir<n)   
		{   
			if(!out[index])   ++cir;     
			index=(index+1)%m;                       
		}             
		cir=0;   
		++step;   
		out[(index+m-1)%m]=1;  
		int tmp = !index?m:index;
		cout<<"The "<<tmp<<" is killed.\n";   
	}         
	free(out); 
	cout<<endl;
	return;
}

void Match(){
	int stick_num;
	printf("Please input the total number of the stick :");
	scanf("%d",&stick_num);

	int take_max = 4;
	printf("Input the max number each time to take:");
	scanf("%d",&take_max);

	for(int lastNum=1; lastNum<=take_max ; lastNum++)
	{
		for(int firstNum=1; firstNum<=take_max ; firstNum++)
		{
			if( (stick_num-lastNum-firstNum)% (take_max+1) !=0 )
			{
			}
			else
			{
				cout<<"\nThe solution:\n";
				cout<<"A first take the "<<firstNum<<endl;
				srand((unsigned)NULL);  
				for(int m=0; m< (stick_num-lastNum-firstNum)/ (take_max+1); m++)
				{
					int  i=int(rand()% (take_max))+1; 
					cout<<"Then B take "<<(take_max+1)-i<<" away!"<<endl;
				}
				cout<<"A take "<<lastNum<<" away!"<<endl;
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -