题意
题目的意思是在桌上叠卡片,最上一层需要覆盖次一层的1/2, 次一层需要覆盖次二层的2/3,最底层覆盖卡子占卡片的n - 1 / n,给定长度,就是从桌面右边到最顶层卡片右边的距离,求需要多少张卡片才够。
分析
换句话说就是根据1/2 + 1/3 + … + (n-1)/n的范围,求得n。
由于长度在0.01~5.20之间,直接打表即可。
用数组下标记录长度会省去后面的搜索时间。
代码如下:
Memory: 240K Time: 0MS Length:15LINES
#include<iostream>
using namespace std;
int main()
{
int Mapping[520] = { 0 };
int i = 0, n = 3;
double num = 0.5;
while (i < 520)
{
while (double(i + 1) <= num * 100) Mapping[i++] = n - 2;
num += 1.0 / n++;
}
while (cin >> num && num != 0.0) printf("%d card(s)\n", Mapping[int(num * 100) - 1]);
return 0;
}