poj 2967 Triangles 排序

来源:互联网 发布:阿里云企业网站 编辑:程序博客网 时间:2024/04/28 09:32

题意:

给n跟木棒,问他们是否满足1.其中3根能组成三角形,2.不是任意3根都能组成三角形。

分析:

对于a<=b<=c,他们能组成3角形的充要条件是a+b>c,对整个数组排序后判断即可。这题用c++提交会超时,要用g++。

代码:

//poj 2967//sep9#include <iostream>#include <algorithm>#include <cstdio>using namespace std;int a[1000024]; int n;char ch;  int scan()  {      int res;      while( ch = getchar(), ch < '0' || ch > '9' );         res = ch - '0';      while( ch = getchar(), ch >= '0' && ch <= '9' )          res = res*10 + ch - '0';      return res;  } bool work(){if(a[0]+a[1]>a[n-1])return false;for(int i=0;i<n-2;++i)if(a[i]+a[i+1]>a[i+2])return true;return false;}int main(){n=scan();int i;for(i=0;i<n;++i)a[i]=scan();sort(a,a+n);if(work())printf("The set is accepted.\n");elseprintf("The set is rejected.");return 0;} 


0 0