1001-度度熊拼三角
题目:
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
思路:
简单贪心,先将木棒按照长度sort 一遍,在从最长的开始向前取,如果能组成三角形就是答案
代码:
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1e3+30; int a[maxn]; int ok(int a, int b, int c) { return a+b>c && a+c > b && b+c > a; } int main() { int n; while(~scanf("%d", &n)) { for (int i = 1 ; i <= n ; i ++) { scanf("%d", a+i); } sort(a+1, a+1+n); int ans = -1; for (int i = n ; i >= 3 ; i --) { if (ok(a[i], a[i-1], a[i-2])) { ans = a[i] + a[i-1] + a[i-2]; break; } } printf("%d ", ans); } return 0; }