首先題目要求保留小數點後5位,那麼當n大到一定程度,前5位是不會再改變了。對於會變的情況直接打表預處理,大於則直接輸出不變的結果。題目很坑,沒說n的範圍,需要用字符串讀入。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define LL long long
const int maxn=1000010;
const int c=130000;
char s[maxn];
double f[c];
void init()
{
f[1]=1.0;
for(int i=2;i<c;i++)
{
f[i]=f[i-1]+1.0/(i*1.0)/(i*1.0);
}
}
int main()
{
init();
int res;
while(~scanf("%s",&s))
{
int len=strlen(s);
if(len>6) printf("%.5f\n",f[c-1]);
else
{
res=0;
for(int i=0;i<len;i++)
{
res=res*10+s[i]-'0';
}
if(res>c) printf("%.5f\n",f[c-1]);
else printf("%.5f\n",f[res]);
}
}
return 0;
}