【POJ1003】Hangover(二分搜索)

直接用库函数二分即可。

 

 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdlib>

 4 #include <cstdio>

 5 #include <cmath>

 6 #include <cctype>

 7 #include <algorithm>

 8 #include <numeric>

 9 

10 #define eps 1e-8

11 using namespace std;

12 

13 double len[500];

14 

15 int main () {

16     ios :: sync_with_stdio(false);

17     len[0] = 0;

18     for (int i = 1; i < 300; ++ i) {

19         len[i] = len[i - 1] + 1.0 / (double)(i + 1);

20     }

21 

22     //cout << len[273] << endl;

23     double n;

24     while (cin >> n) {

25         if (n - 0 < eps) {

26             break;

27         }

28         //cout << lower_bound(len, len + 300, n) - &len[0] << endl;

29         printf ("%d card(s)\n", lower_bound(len, len + 300, n) - &len[0]);

30     }

31     return 0;

32 }

 

你可能感兴趣的:(over)