题目管理 1184: Make an Equilateral Triangle

3 篇文章 0 订阅

题目管理 1184: Make an Equilateral Triangle

时间限制: 2 Sec    内存限制: 256 MB 
提交: 30    解决: 4
Equilateral Triangle refers to a triangle with three same side of length. There are some sticks with various length (L stands for length,0<L<100). Is it possible to make an Equilateral Triangle with those sticks?

输入

(1)The first line of input contains N(N = the number of test cases).
(2)Each test case begins with an integer M(3 <= M <= 20, the number of sticks). M integers follow; each gives the length of a stick - an integer between 1 and 99. 

输出

For each case, output a line containing "yes" if is is possible to form an Equilateral Triangle; otherwise output "no". 

样例输入

5
3 2 2 2
4 4 3 2 1 
5 1 2 3 4 5
6 1 7 2 6 3 5
7 3 7 4 3 9 2 2 

样例输出

yes
no
yes
yes
no



/*************************************************************************
    > File Name: main.cpp
    > Author: susecjh
    > Mail: susecjh@gmail.com 
    > Created Time: 2017年06月09日 星期五 07时07分55秒
 ************************************************************************/

#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#define LOCAL

class stick{
public:
	stick(int length){
		this -> length = length;
		this -> used = false;
	}
	bool getUsed(){
		return this -> used;
	}
	int getLength(){
		return this -> length;
	}
	void beUse(){
		this -> used = true;
	}
	void reUse(){
		this -> used = false;
	}
private:
	int length;
	bool used;
};

using namespace std;

vector<int> flags;


bool find(vector<stick> &sticks,int avg,int begin);
bool cmp(stick &a,stick &b);
void clearFlags(vector<stick> &);

int main(int argc,char **argv)
{
#ifdef LOCAL
	ifstream cin("data.in");
#endif
	vector<stick> sticks;
	int n,m,len;
	int sum,avg,count;
	cin>>n;
	for(int i = 0; i < n; i++){
		cin>>m;
		sticks.clear();
		for(int j = 0; j < m; j++){
			cin>>len;
			sticks.push_back(stick(len));
		}
		sum = 0;
		for(vector<stick>::iterator it = sticks.begin(); it != sticks.end(); it++){
			sum += it->getLength();
		}
		if(sum%3 != 0){
			cout<<"no"<<endl;
			continue;
		}
		sort(sticks.begin(),sticks.end(),cmp);
		avg = sum /3;
		bool flag;
		flag = false;
		if(find(sticks,avg,0))
			if(find(sticks,avg,0))
				flag = true;
		if(flag){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
	}
	return 0;
}

bool find(vector<stick> &sticks,int avg,int begin)
{
	for(int i = begin; i < sticks.size(); i++){
		if(sticks[i].getUsed()){
			continue;
		}
		if(sticks[i].getLength()==avg){
			flags.push_back(i);
			clearFlags(sticks);
			return true;
		}else if(sticks[i].getLength()< avg){
			flags.push_back(i);
			for(int j = i+1; j < sticks.size(); j++){
				if(!find(sticks,avg-sticks[i].getLength(),j)){
					continue;
				}else{
					flags.push_back(j);
					clearFlags(sticks);
					return true;
				}
			}
			flags.pop_back();
		}
	}
	return false;
}

bool cmp (stick &a,stick &b)
{
	return a.getLength() > b.getLength();
}

void clearFlags(vector<stick> &sticks)
{
	for(int i = 0; i < flags.size(); i++){
		sticks[flags[i]].beUse();
	}
	flags.clear();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值