C++经典编程题目(十二)硬币翻转问题

有N个硬币(N为偶数)正面朝上排成一排,每次将 N-1 个硬币翻过来放在原位
 置, 不断地重复上述过程,直到最后全部硬币翻成反面朝上为止。编程让计算机把
 翻币的最简过程及翻币次数打印出来(用*代表正面,O 代表反面)。

#include "stdio.h"
#include 
using namespace std;

/*
此方案用于做题足够了,但是还有一些问题应当思考。

问题:此程序并不是由程序自己推导出最优方法,而是人为预设,可思考改进。
*/

void main()
{
     
	int n;
	cout<<"Please input the number of coin (Even numbers):"<<endl;
	cin >> n;
	if (n%2)
	{
     
		cout << "Error input !" << endl;
	}

	bool *str = new bool[n];

	for (int i = 0; i < n; i++)
	{
     
		str[i] = true;
	}

	for (int i = 0; i < n; i++)
	{
     
		for (int j = 0; j < n; j++)
		{
     
			if (j!=i)
			{
     
				str[j] = !str[j];
			}
			printf("%3c", str[j] ? '*' : '0');
		}
		printf("\n");
	}
	system("pause"); 
}

你可能感兴趣的:(C++学习笔记)