zzuli2183—分步打表

来源:互联网 发布:手机免费炒股软件 编辑:程序博客网 时间:2024/05/22 10:41

河南省第五次多校联萌B题
2183: 就是签到题XD

Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 521 Solved: 78

SubmitStatusWeb 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

Source

河南省多校连萌(五)

SubmitStatusWeb Board
解题思路:
分步打表,把10000000分为50个一组,整50的存一次,打完表之后每一次最多循环50次·。(受到大佬的启发才知道怎么做的)
下面是代码:

#include<stdio.h>#define N 10000000double f[N/50+3];void db();int main(){    int t,n,i;    db();    scanf("%d",&t);    int k=0;    while(t--)    {        k++;        scanf("%d",&n);        double v=f[n/50];        for(i=n/50*50+1;i<=n;i++)        v+=1.0/double(i);        printf("Case %d: %.10lf\n",k,v);    }    return 0;}void db(){    double s=1.0;    int i;    f[1]=1.0;    for(i=2;i<=N;i++)    {        s+=1/double(i);        if(i%50==0)        f[i/50]=s;    }}/**************************************************************    Problem: 2183    User: 201605050139    Language: C++    Result: Accepted    Time:126 ms    Memory:2452 kb****************************************************************/

上面的代码能交过c++交不过zzuli的c,下面这个能交过c

#include<stdio.h>#define N 10000003double f[N/50+3];void db();int main(){    int t,n,i;    db();    double c;    scanf("%d",&t);    int k=0;    while(t--)    {        k++;        scanf("%d",&n);        double v=f[n/50];        for(i=n/50*50+1;i<=n;i++)        {            c=i;            v+=1.0/c;        }        printf("Case %d: %.10lf\n",k,v);    }    return 0;}void db(){    double s=1.0;    int i;    double c;    f[1]=1.0;    for(i=2;i<=N;i++)    {        c=i;        s+=1.0/c;        if(i%50==0)        f[i/50]=s;    }}
原创粉丝点击