hdu 5879 Cure

Problem Description
Given an integer  n, we only want to know the sum of 1/k2 where k from 1 to n.
 

 

Input
There are multiple cases.
For each test case, there is a single line, containing a single positive integer  n
The input file is at most 1M.
 

 

Output
The required sum, rounded to the fifth digits after the decimal point.
 

 

Sample Input
1 2 4 8 15
 

 

Sample Output
1.00000 1.25000 1.42361 1.52742 1.58044
 

 

Source
首先他说输入文件不超过1m,每个样例就是一个数,可见样例会很大,所以最好是打表,排着算一定超时,再者没有给出n的范围,输出要求保留五位小数,如果n特别大后面估计都是一样的,通过打表就可以发现,我们只需要打1000000以内的即可,实际可以更小,因为从某个位置开始的结果都是1.64493,所以分情况来讨论。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 10000000
#define DMAX 1000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
char num[MAX + 1];
double sum[DMAX];
int main() {
    for(long long i = 1;i < 1000000;i ++) {
        sum[i] = sum[i - 1] + 1.0 / (i * i);
    }
    while(~scanf("%s",num)) {
        double ans = 0;
        if(strlen(num) > 6) ans = 1.64493;
        else {
            int d = 0;
            for(int i = 0;num[i];i ++) {
                d = d * 10 + num[i] - '0';
            }
            ans = sum[d];
        }
        printf("%.5f\n",ans);
    }
}

 

转载于:https://www.cnblogs.com/8023spz/p/10028934.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值