翻硬币问题

来源:互联网 发布:seo研究中心是什么 编辑:程序博客网 时间:2024/05/17 03:53

一个数学问题
七枚硬币,每次翻两个,问能不能全部翻成正面
肯定不能
不管怎么翻 都是偶数个朝上
一开始也没细想
直接编程
顺便写了两个二进制模板吧
感觉和状态压缩也有一些联系

#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);}
0 0
原创粉丝点击