首先看到這個題目,是個比較簡單的題目,但是有些細節沒有把握好,老是運行錯誤,發一下題目的鏈接吧!
http://acm.nyist.net/JudgeOnline/problem.php?pid=6
自己編譯的沒有通過,主要是沒有考慮到寬度的問題,然後長度方面也沒有考慮細緻,當然我也是利用sort排序了的,這個很方便,不用那麼受限制,從大到小依次來的。
貼一下AC過了代碼吧!
#include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; int cmp( const double &a, const double &b ) { if( a > b ) return 1; else return 0; } int main( int argc, char **argv ) { int n; scanf("%d", &n); double buf[600]; // 草地斜邊長度的一半 double len = sqrt(20*20 + 2*2)/2; while ( n-- ) { int m; scanf("%d", &m); for ( int i = 0; i < m; ++i ) scanf("%lf", buf+i); // 從大到小排 sort (buf, buf+m, cmp); double sum = 0; int i; for ( i = 0; i < m; ++i ) { if (buf[i] <= 1) break; sum += buf[i]; if ( sum >= len ) // 半徑和大於等於草地斜邊長度一半就滿足條件了 break; } printf( "%d\n", i+1); } return 0; }