NYOJ-幸运三角形

太坑!  打表的时候仔细点。

打表代码:

 1 #include <stdio.h>
 2 #include <memory.h>
 3 #include <math.h>
 4 int cnt,n;
 5 int cnt_of_1,cnt_of_0;
 6 int ok;///记录完美三角形的个数
 7 void next(char a[],int m){///完成大数+1,(从后算)
 8     ///大数 + 1
 9     a[m-1]++;
10     int j = m-1;
11     while(a[j] > '1'){
12         a[j] = '0';
13         a[j-1]++;
14         j--;
15     }
16 }
17 void count(char a[],int m){
18     ///统计个数
19     for(int j = m-1; j >= 0; --j){
20         if(a[j] == '1')
21             cnt_of_1++;
22         else
23             cnt_of_0++;
24     }
25 }
26 void dfs(char a[],int m){///由字符串 a 推出字符串 b; m为字符串a的长度
27     count(a,m);
28     if(cnt_of_1 > cnt / 2 || cnt_of_0 > cnt / 2 || m < 0)
29         return ;
30     if(m == 1 &&cnt_of_0 == cnt_of_1){
31         ok++;
32         return ;
33     }
34     for(int i = 0; i < m-1; i++){
35         a[i] = (a[i] == a[i+1]) ? '1' : '0';
36     }
37     dfs(a,m-1);
38 }
39 int main(void){
40     char a[25],b[25];
41     while(scanf("%d",&n) != EOF){
42         cnt = n*(1+n)/2;
43         if(cnt % 2 == 1){
44             printf("0
");
45             continue;
46         }
47         ok=0;
48         memset(b,'0',25*sizeof(int));
49         for(int i = pow(2,n); i > 0; i--){
50             for(int j = 0; j < n; j++)
51                 a[j] = b[j];
52             cnt_of_0 = cnt_of_1 = 0;
53             dfs(a,n);
54             next(b,n);///查找a+1
55         }
56         printf("%d
",ok);
57     }
58     return 0;
59 }

提交代码:

 1 #include <stdio.h>
 2 int a[25]={0, 0, 0, 4, 6, 0, 0, 12, 40, 0, 0, 171, 410, 0, 0,
 3             1896, 5160, 0, 0, 32757, 59984, 0, 0, 431095, 822229};
 4 int main(void){
 5     int n;
 6     while(scanf("%d",&n) != EOF){
 7         printf("%d
",a[n]);
 8     }
 9     return 0;
10 }
原文地址:https://www.cnblogs.com/yfs123456/p/5618496.html