POJ1064
1 #include <iostream> 2 #include <iomanip> 3 #include <cmath> 4 using namespace std; 5 6 int N; 7 int K; 8 double num[10001]; 9 10 int calc(double len) 11 { 12 int numK= 0; 13 for(int i = 0; i < N; ++i) 14 { 15 numK += (int)(num[i] / len); 16 } 17 return numK; 18 } 19 20 double solve() 21 { 22 double low = 0; 23 double upper = 100000; 24 int count = 0; 25 double mid; 26 int numK; 27 while(count < 100) 28 { 29 mid = (low + upper) / 2; 30 numK = calc(mid); 31 if(numK >= K) 32 low = mid; 33 else 34 upper = mid; 35 ++count; 36 } 37 return mid; 38 } 39 40 int main() 41 { 42 //基于二分法的思想实现 43 cin >> N >> K; 44 for(int i = 0; i < N; ++i) 45 cin >> num[i]; 46 //这里floor很重要 47 cout << setiosflags(ios::fixed) << setprecision(2) << floor(solve()*100)/100 << endl; 48 return 0; 49 }