本文主要是介绍poj_1003——Hangover,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
地址:
http://poj.org/problem?id=1003
题意很简单,输入一个数, 算出1/2 + 1/3 + 1/4 + ... + 1/(n + 1)大于这个数时的最小n。
用了一个很暴力的方法就是打表;
#include<iostream>
using namespace std;
double c[296]={0.0,
0.5,0.833333,1.08333,1.28333,1.45,1.59286,1.71786,
1.82897,1.92897,2.01988,2.10321,2.18013,2.25156,2.31823,2.38073,2.43955,
2.49511,2.54774,2.59774,2.64536,2.69081,2.73429,2.77596,2.81596,2.85442,
2.89146,2.92717,2.96165,2.99499,3.02725,3.0585,3.0888,3.11821,3.14678,
3.17456,3.20159,3.2279,3.25354,3.27854,3.30293,3.32674,3.35,3.37273,
3.39495,3.41669,3.43796,3.4588,3.47921,3.49921,3.51881,3.53804,3.55691,
3.57543,3.59361,3.61147,3.62901,3.64625,3.6632,3.67987,3.69626,3.71239,
3.72827,3.74389,3.75928,3.77443,3.78935,3.80406,3.81855,3.83284,3.84692,
3.86081,3.87451,3.88802,3.90136,3.91451,3.9275,3.94032,3.95298,3.96548,
3.97782,3.99002,4.00207,4.01397,4.02574,4.03736,4.04886,4.06022,4.07146,
4.08257,4.09356,4.10443,4.11518,4.12582,4.13635,4.14676,4.15707,4.16728,
4.17738,4.18738,4.19728,4.20708,4.21679,4.22641,4.23593,4.24536,4.25471,
4.26397,4.27314,4.28223,4.29124,4.30017,4.30902,4.31779,4.32649,4.33511,
4.34366,4.35213,4.36053,4.36887,4.37713,4.38533,4.39346,4.40152,4.40952,
4.41746,4.42533,4.43315,4.4409,4.44859,4.45622,4.4638,4.47132,4.47878,
4.48619,4.49354,4.50084,4.50809,4.51528,4.52242,4.52952,4.53656,4.54355,
4.5505,4.55739,4.56424,4.57104,4.5778,4.58451,4.59118,4.5978,4.60438,
4.61092,4.61741,4.62386,4.63027,4.63664,4.64297,4.64926,4.65551,4.66172,
4.66789,4.67403,4.68013,4.68619,4.69221,4.6982,4.70415,4.71007,4.71595,
4.7218,4.72761,4.73339,4.73914,4.74486,4.75054,4.75619,4.76181,4.76739,
4.77295,4.77847,4.78397,4.78943,4.79487,4.80027,4.80565,4.811,4.81631,
4.82161,4.82687,4.8321,4.83731,4.84249,4.84765,4.85278,4.85788,4.86296,
4.86801,4.87303,4.87803,4.88301,4.88796,4.89288,4.89779,4.90266,4.90752,
4.91235,4.91716,4.92194,4.9267,4.93144,4.93616,4.94085,4.94553,4.95018,
4.95481,4.95941,4.964,4.96857,4.97311,4.97764,4.98214,4.98663,4.99109,
4.99554,4.99996,5.00437,5.00875,5.01312,5.01747,5.0218,5.02611,5.0304,
5.03467,5.03893,5.04316,5.04738,5.05159,5.05577,5.05994,5.06409,5.06822,
5.07233,5.07643,5.08051,5.08458,5.08863,5.09266,5.09667,5.10068,5.10466,
5.10863,5.11258,5.11652,5.12044,5.12434,5.12824,5.13211,5.13597,5.13982,
5.14365,5.14747,5.15127,5.15506,5.15883,5.16259,5.16634,5.17007,5.17378,
5.17749,5.18118,5.18485,5.18852,5.19217,5.1958,5.19943,5.20304,5.20663,
5.21022,5.21379,5.21735,5.22089,5.22443,5.22795,5.23146,5.23495,5.23844,
5.24191,5.24537,5.24882,5.25226,5.25568,5.25909,5.2625,5.26589,5.26926,};
int main()
{int i;double a;while(cin>>a,0){for(i=0;c[i]<a;)i++;cout<<i<<" card(s)\n";}return 0;
}
后来又换了一种
#include<iostream>
using namespace std;
int main()
{float b;int n;while(cin>>b,b){for(n=1;b>0;)b-=1./++n;cout<<n-1<<" card(s)\n";}return 0;
}
这篇关于poj_1003——Hangover的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!