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

📄 turncoin.cpp

📁 翻币问题:有n(n>=6)个硬币
💻 CPP
字号:
#include "iostream"
using namespace std;

class Compute{
private:
	int n;
public:
	Compute(int k = 0)
	{
		n = k;
	}
	void operation()
	{
		int t = n % 5;//t代表最后剩余的小于5的正面朝上的硬币数目
		int step = n / 5;//翻硬币的次数
		if (t == 0)
		{
			int i;
			cout<<"total times are:"<<n / 5<<endl;//打印所需的总共次数
			for (i = 1; i <= step; i++)//打印每次翻的结果
			{
				cout<<"the "<<i<<" time ";
				cout<<i*5<<" coins are reversed and "<<n - i * 5<<" coins are front"<<endl;
			}
		}
		if (t == 1)
		{
			int i;
			if (n == 6)
			{
				cout<<"total times are:"<<6<<endl;
				for (i = 1; i <= 6; i++)
				{
					switch(i)
					{
					case 1:
						cout<<"the "<<i<<" time ";
						cout<<5<<" coins are reversed and "<<1<<" coins are front"<<endl;
						break;
					case 2:
						cout<<"the "<<i<<" time ";
						cout<<2<<" coins are reversed and "<<4<<" coins are front"<<endl;
						break;
					case 3:
						cout<<"the "<<i<<" time ";
						cout<<3<<" coins are reversed and "<<3<<" coins are front"<<endl;
						break;
					case 4:
						cout<<"the "<<i<<" time ";
						cout<<4<<" coins are reversed and "<<2<<" coins are front"<<endl;
						break;
					case 5:
						cout<<"the "<<i<<" time ";
						cout<<1<<" coins are reversed and "<<5<<" coins are front"<<endl;
						break;
					case 6:
						cout<<"the "<<i<<" time ";
						cout<<6<<" coins are reversed and "<<0<<" coins are front"<<endl;
						break;
					default:
						break;
					}
				}
			}
			else
			{
				cout<<"total times are:"<<n / 5 + 1;
				for ( i = 1; i < step; i++)
				{
					cout<<"the "<<i<<" time ";
					cout<<i*5<<" coins are reversed and "<<n - i * 5<<" coins are front"<<endl;
				}
				for ( i = step; i <= step + 1; i++ )
				{
					switch(i - step)
					{
					case 0:
						cout<<"the "<<i<<" time ";
						cout<<n - 5<<" coins are reversed and "<<5<<" coins are front"<<endl;
						break;
					case 1:
						cout<<"the "<<i<<" time ";
						cout<<n<<" coins are reversed and "<<0<<" coins are front"<<endl;
						break;
					default:
						break;
					}
				}
			}
		}
		if (t == 2)
		{
			int i;
			cout<<"total times are:"<<n / 5 + 2<<endl;
			for ( i = 1; i < step + 1; i++)
				{
					cout<<"the "<<i<<" time ";
					cout<<i*5<<" coins are reversed and "<<n - i * 5<<" coins are front"<<endl;
				}
				for ( i = step + 1; i <= step + 2; i++ )
				{
					switch(i - step)
					{
					case 1:
						cout<<"the "<<i<<" time ";
						cout<<n - 5<<" coins are reversed and "<<5<<" coins are front"<<endl;
						break;
					case 2:
						cout<<"the "<<i<<" time ";
						cout<<n<<" coins are reversed and "<<0<<" coins are front"<<endl;
						break;
					default:
						break;
					}
				}
		}
		if ( t == 3)
		{
			int i;
			if (n == 8)
				{
					cout<<"total times are:"<<4<<endl;
					for (i = 1; i <= 4; i++)
					{
						switch(i)
						{
						case 1:
							cout<<"the "<<i<<" time ";
							cout<<5<<" coins are reversed and "<<3<<" coins are front"<<endl;
							break;
						case 2:
							cout<<"the "<<i<<" time ";
							cout<<6<<" coins are reversed and "<<2<<" coins are front"<<endl;
						    break;
						case 3:
							cout<<"the "<<i<<" time ";
						    cout<<3<<" coins are reversed and "<<5<<" coins are front"<<endl;
						    break;
						case 4:
							cout<<"the "<<i<<" time ";
						    cout<<8<<" coins are reversed and "<<0<<" coins are front"<<endl;
						    break;
						default:
							break;
						}
					}
			}
			else
			{
				cout<<"total times are:"<<n / 5 + 1<<endl;
				for ( i = 1; i < step; i++)
				{
					cout<<"the "<<i<<" time ";
					cout<<i*5<<" coins are reversed and "<<n - i * 5<<" coins are front"<<endl;
				}
				for ( i = step; i <= step + 1; i++ )
				{
					switch(i - step)
					{
					case 0:
						cout<<"the "<<i<<" time ";
						cout<<n - 5<<" coins are reversed and "<<5<<" coins are front"<<endl;
						break;
					case 1:
						cout<<"the "<<i<<" time ";
						cout<<n<<" coins are reversed and "<<0<<" coins are front"<<endl;
						break;
					default:
						break;
					}
				}
			}
		}
		if (t == 4)
		{
			int i;
			cout<<"total times are:"<<n / 5 + 2<<endl;
			for ( i = 1; i < step + 1; i++)
				{
					cout<<"the "<<i<<" time ";
					cout<<i*5<<" coins are reversed and "<<n - i * 5<<" coins are front"<<endl;
				}
				for ( i = step + 1; i <= step + 2; i++ )
				{
					switch(i - step)
					{
					case 1:
						cout<<"the "<<i<<" time ";
						cout<<n - 5<<" coins are reversed and "<<5<<" coins are front"<<endl;
						break;
					case 2:
						cout<<"the "<<i<<" time ";
						cout<<n<<" coins are reversed and "<<0<<" coins are front"<<endl;
						break;
					default:
						break;
					}
				}
		}
  }
};

 int main()
{
	int n;
	cout<<"please input the number of fronted coins:";
	cin>>n;
	if (n < 6)
	{
		cout<<"error"<<endl;
	}
	else
	{
		cout<<"the least steps fuction is"<<endl;
		Compute a1(n);
		a1.operation();
	}
	return 0;
 }

⌨️ 快捷键说明

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