HDU 5879(打表)

题意:给出n,计算1/i*i (1<=i<=n)的和,保留5位小数

方法:数据很大,用字符串存储,因为\sum1/i^2是收敛的,所以当i无穷大时(这里大约是1000000),收敛到一个值了。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1000010;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
double sum[1000010];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    string n;
    for(int i=1;i<maxn;i++)
    {
        sum[i]+=sum[i-1]+1.0/i/i;
    }
    while(cin>>n)
    {
        if(n.size()>=7)
            cout<<fixed<<setprecision(5)<<sum[maxn-10]<<endl;
        else
            {
                ll g=0;
                for(int i=0;i<n.size();i++)
                {
                    g=g*10+n[i]-'0';
                }
                cout<<fixed<<setprecision(5)<<sum[g]<<endl;
            }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/81675229