您的位置:首页 > 其它

翻硬币问题

2016-08-11 22:54 246 查看
一个数学问题

七枚硬币,每次翻两个,问能不能全部翻成正面

肯定不能

不管怎么翻 都是偶数个朝上

一开始也没细想

直接编程

顺便写了两个二进制模板吧

感觉和状态压缩也有一些联系

#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 1<<7;
bool b[MAXN];
int a[7];
void toarr(int x) {
for (int i = 0; i < 7; i++) {
a[i] = x % 2;
x /= 2;
}
}
int to2()
{
int x = 0;
for (int i = 0; i < 7; i++) {
x += (a[i] << i);
}
return x;
}
void print()
{
for (int i = 0; i < 7; i++) {
cout << a[i];
}
cout << endl;
}
void dfs(int x) {
print();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < i; j++) {
a[i] = 1 - a[i];
a[j] = 1 - a[j];
int t = to2();
if (!b[t]) {
b[t] = true;
dfs(to2());
}
a[i] = 1 - a[i];
a[j] = 1 - a[j];
}
}
}
int main()
{
memset(b, 0, sizeof(b));
b[0] = true;
dfs(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: