等边三角形

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/amazingcode/article/details/79585194

蒜头君手上有一些小木棍,它们长短不一,蒜头君想用这些木棍拼出一个等边三角形,并且每根木棍都要用到。 例如,蒜头君手上有长度为 11223333 的4根木棍,他可以让长度为1122 的木棍组成一条边,另外 22 跟分别组成 22 条边,拼成一个边长为 33 的等边三角形。蒜头君希望你提前告诉他能不能拼出来,免得白费功夫。

输入格式

首先输入一个整数 n(3≤n≤20)n(3n20),表示木棍数量,接下来输入 nn 根木棍的长度 pi(1≤pi≤10000)pi(1pi10000)

输出格式

如果蒜头君能拼出等边三角形,输出"yes",否则输出"no"

样例输入1

5
1 2 3 4 5

样例输出1

yes

样例输入2

4
1 1 1 1

样例输出2

no
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<set>
#include<list>
#include<stack>
#include<cctype>
#include<map>
#include<queue>
using namespace std;
int n,k;
int a[25];
int edge=0,flag=0;
void dfs(int i,int x,int y,int z)
{
    if(x>k||y>k||z>k)
        return ;
    if(i>=n||flag)
        return ;
    if(x==k&&y==k)
    {
        flag=1;
        cout<<"yes"<<endl;
        return ;
    }
    dfs(i+1,x+a[i],y,z);
    dfs(i+1,x,y+a[i],z);
    dfs(i+1,x,y,z+a[i]);
//    return 0;
}
int main()
{
    int sum=0;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    sort(a,a+n);
    k=sum/3;
    if(sum%3!=0)
    {
        cout<<"no"<<endl;
        return 0;
    }
    for(int j=0;j<n;j++)
    {
        if(a[j]>k)
        {
            cout<<"no"<<endl;
            return 0;
        }
    }
    dfs(0,0,0,0);
    if(!flag)
        cout<<"no"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/amazingcode/article/details/79585194