有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");
}