zl程序教程

您现在的位置是:首页 >  其它

当前栏目

刷题记录:牛客NC204117牛牛的三角形

记录 刷题 牛客 三角形
2023-09-14 09:12:53 时间

传送门:牛客
题目描述:

牛牛有一个数组长度大小为n,数组中有n个正整数。现在牛牛请你从其中选出三个元素(注意选择元素的下标不能相同,但是其值可以相同)组成一个三角形。
无法做到,请输出一行一个字符串"No solution",反之请输出这三个元素的值。
如果有多种组成三角形的元素组合,你可以输出任意一种
输出:
5
2 2 3 2 2
输出:
2 2 3

一星水题,n的大小不大,直接暴力三层循环枚举即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
	ll x=0,w=1;char ch=getchar();
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
	return x*w;
}
int a[200];
#define maxn 1000000
int main() {
	int n;n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++) {
		for(int j=i+1;j<=n;j++) {
			for(int k=j+1;k<=n;k++) {
				int maxx=max(max(a[i],a[j]),a[k]);
				int sum=a[i]+a[j]+a[k];
				if(sum-maxx>maxx) {
					cout<<a[i]<<" "<<a[j]<<" "<<a[k]<<endl;
					return 0;
				}
			}
		}
	}
	cout<<"No solution"<<endl;
	return 0;
}