【水搜索】#31 A. Worms Evolution

本文介绍了一种算法,用于解决一个关于蠕虫形态的研究问题。教授Vasechkin提出了一个理论,即所有蠕虫通过分裂进化,并需要找到三种特定的蠕虫形态,使得其中一种的长度等于另外两种长度之和。文章提供了一个简单有效的解决方案,采用三层循环来验证这一条件。

A. Worms Evolution
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are nforms of worms. Worms of these forms have lengths a1a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.

Input

The first line contains integer n (3 ≤ n ≤ 100) — amount of worm's forms. The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — lengths of worms of each form.

Output

Output 3 distinct integers i j k (1 ≤ i, j, k ≤ n) — such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.

Sample test(s)
input
5
1 2 3 5 7
output
3 2 1
input
5
1 8 1 5 1
output
-1

还亏我想了半天……这道题暴力O (n^3)都能无压力A……

那就直接水掉吧~ FORFORFOR 检测flag~ over~

Code:8k 30ms AC

#include <cmath>   
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <iostream>  
#include <algorithm>
#define FOR(a,b) for(a=b;a<num;a++)
using namespace std;
int list[101];
int main()
{
	int h,i,j,k,num=0;
	scanf("%d",&num);
	memset(list,0,sizeof list);
	FOR(h,0)	scanf("%d",&list[h]);
	int flag=0;
	FOR(i,0)
	{
		FOR(j,i+1)
		{
			FOR(k,j+1)
			{
				flag= 	(list[i]==list[j]+list[k])?1:
						(list[j]==list[k]+list[i])?2:
						(list[k]==list[i]+list[j])?3:0;	
				if(flag)break;
			}
			if(flag)break;
		}
		if(flag)break;
	}
	if(flag)
	{
		int a = (flag==1)?i:(flag==2)?j:k;
		int b = (flag==1)?j:(flag==2)?k:i;
		int c = (flag==1)?k:(flag==2)?i:j;
		printf("%d %d %d",a+1,b+1,c+1);
	}
	else printf("-1");
	return 0;
}





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值