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

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

#include "stdio.h"
#include <iostream>
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"); 
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值