计蒜_统计三角形

来源:互联网 发布:淘宝客佣金从哪里扣的 编辑:程序博客网 时间:2024/04/30 13:06
两个人有N根不同长度的木棍,看谁能猜出这些木棍一共能拼出多少个不同的不等边三角形。拼三角形的时候要用上所有的木棍(即每个三角形都要用到所有的木棍)。不同的定义是至少有一条边的长度不相同,不等边的定义是三条边都不相等。题目大意是用n根木棍能组成多少个不同的三角形,每个三角形都要用上全部的木棍。#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n, l[15];bool h[10000];bool is_triangle(int a, int b, int c){    return !h[a * 100 + b] && a && b && c && a+b > c && a+c > b && b+c > a && (h[a*100+b] = true);}int dfs(int index, int a, int b, int c){    if(index == n){        return a < b && b < c && is_triangle(a, b, c);   比如 3,4,5和4,3,5,这两个同样的三角形在这里被过滤掉其中一个,保留3,4,5这个形式。    }    return dfs(index + 1, a + l[index], b, c)        + dfs(index + 1, a, b + l[index], c)        + dfs(index + 1, a, b, c + l[index]);}int main(){    int T;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        for(int i = 0; i < n; ++i)        {            scanf("%d", &l[i]);        }        memset(h, 0, sizeof(h));        printf("%d\n", dfs(0, 0, 0, 0));    }    return 0;}

0 0