计蒜客课程竞赛入门--统计三角形 代码流程摘记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计蒜客课程竞赛入门--统计三角形 代码流程摘记相关的知识,希望对你有一定的参考价值。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int n,l[15];//n:木棍数量,l数组:每根木棍长度
 6 bool h[10000];
 7 /*
 8 因为三条边长度是确定的,所以确定两条边就可以,且,这个数组是用哈希表来储存
 9 储存方式为第一条边长度*100+第二条边长度,最高位99*100+99=9999,故存10000
10 */
11 bool is_triangle(int a,int b,int c)
12 {
13   return !h[a*100+b]&&a&&b&&c&&a+b>c&&a+c>b&&b+c>a&&(h[a*100+b]=true);      
14 }//两边之和大于第三边,之前判断是判断是否已经进行过判断,判断后将该处置为true
15 
16 //dfs深度搜索来枚举所有情况
17 int dfs(int index,int a,int b,int c)//index:边界条件,当index等于总木柴数跳出递归
18 {
19   if(index==n)
20   { 
21     return a<b&&b<c&&is_triangle(a,b,c);  
22   }         
23   return dfs(index+1,a+l[index],b,c)
24            +dfs(index+1,a,b+l[index],c)
25            +dfs(index+1,a,b,c+l[index]);  
26 }
27 int main()
28 {
29    int T;
30    scanf("%d",&T); 
31    while(T--)
32    {
33       scanf("%d",&n);
34       for(int i=0;i<n;++i)
35        {
36           scanf("%d",&l[i]); 
37        }
38        memset(h,0,sizeof(h));//0为false,即为计算过
39        printf("%d\n",dfs(0,0,0,0));
40    } 
41   return 0;  
42 }

2012年ACM长春赛区现场赛一道题简化而来

以上是关于计蒜客课程竞赛入门--统计三角形 代码流程摘记的主要内容,如果未能解决你的问题,请参考以下文章

计蒜客课程竞赛入门--堆排序 流程记

计蒜客课程竞赛入门--数塔问题(DP) 流程记

计蒜客课程竞赛入门--最长上升子序列(LIS) 流程记

计蒜客课程竞赛入门--蒜头学算数 流程记

计蒜客课程竞赛入门--冗余关系(并查集) 流程记

计蒜客课程竞赛入门--二叉搜索树 流程记