九宫解锁一共有多少种解法?
就像咱们现在的手机那个九宫解锁一共有多少种啊?
#include <stdio.h> int count = 0; int judge(int a[], int len, int key) { int i; if (len && a[0] == key) return 1; for (i = 1;i < len;++i) { if (a[i] == key) return 1; } switch(key) { case 1:case 3:case 7: case 9:return 0; default:break; } for (i = 0;i < len;++i) if (key * 2 == a[i] + a[i-1]) return 1; return 0; } void Output(int a[], int len) { count++; for (int i = 0;i < len-1;++i) printf("%d ", a[i]); printf("%d\n", a[i]); } void fun(int a[], int ip, int all) { if (ip == all) {Output(a, all);return;} for (int i = 1;i < 10;++i) { if (judge(a, ip, i)) continue; a[ip] = i; fun(a, ip+1, all); } } int main() { int i, a[10] = {0}; freopen("out.txt", "w", stdout); for (i = 4;i < 10;++i) fun(a, 0, i); printf("%d\n", count); fclose(stdout); return 0; }