Submit: 545 Solved: 85
SubmitWeb Board
Description
In mathematics, we suppose that f(1)=1, f(i)-f(i-1)=1/i, (2<=i<=n)
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (2 ≤ n ≤10^7).
Output
For each case, print the case number and the value of f(n). The answer should be rounded to 10 decimal places.
Sample Input
3
2
3
4
Sample Output
Case 1: 1.5000000000
Case 2: 1.8333333333
Case 3: 2.0833333333
HINT
题意:有一个数列每一项的值都是前面的数加上其下标分之一。。。。问第n项的值;
思路:由于值比较大,所以采取分段打表的方式,每100项打一次表并以此为基点算值就可以了;
下面附上代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int M=1e7+10;
double a[100010];
void init()
{
double ans=1.0;
for(int i=2;i<=M;i++)
{
ans+=1/double(i);
if(i%100==0)
a[i/100]=ans;
}
}
int main()
{
int T,k=0,n;
cin>>T;
init();
while(T--)
{
cin>>n;
int l=n/100;
double ans=a[l];
for(int i=l*100+1;i<=n;i++)
ans+=1/double(i);
printf("Case %d: %.10lf\n",++k,ans);
}
return 0;
}