2504多项式求和

多项式求和
Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description
多项式描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 ……
先请你求出多项式前n项的和。
Input
第一行输入一个数T代表测试数据个数(T<=1000)。接下来T行每行1个数代表n(0<=n< 2^31)。

Output
对于每个输入样例,输出多项式和的结果(结果精确到小数点后两位)。每行输出一个结果。
Sample Input
2
1
2
Sample Output
1.00
0.50
Hint

Source
中国海洋大学第三届“朗讯杯”编程比赛高级组试题

think:前两个代码是提交后超时的错误代码,最后一个是发现是一个交错级数后修改后的代码,A了

/*//直接相加,利用结构体,链表的作用在哪?然鹅超时
#include<stdio.h>
#include<stdlib.h>
struct node
{
    char fore;
    double data;
    double summ;
    struct node*next;
};
double create(long long int n)
{
    struct node*head,*tail,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    double s=0;
    for(long long int i=1;i<=n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        p->data=1.0/i;
        if(i%2==0)
        {
            p->fore='-';
            s-=p->data;
        }
        else
        {
          p->fore='+';
          s+=p->data;
        }
        p->next=NULL;
        p->summ=s;
        tail->next=p;
        tail=p;
    }
    return tail->summ;
}
int main()
{
    int t;
    long long int n;
    double s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        s=create(n);
        printf("%.2lf\n",s);
    }
    return 0;
}
*/
//利用了递归,可是还是没有找出怎么利用链表,还是超时
//我估计超时的原因是每一次用的时候都得重新算一遍,N的范围
//那么大,怎么做?怎么利用链表的优点?
/*
#include<stdio.h>
#include<stdlib.h>
double s;
struct node
{
    char fore;
    double data;
    int num;
    struct node*next,*upward;
}*tail;
double sum(struct node*tail)
{
    if(tail->num==1)s=1;
    else
    {
        if(tail->num%2==0) s=sum(tail->upward)-tail->data;
        else s=sum(tail->upward)+tail->data;
    }
    return s;
}
int main()
{
    int t;
    long long int n;
    scanf("%d",&t);
    while(t--)
    {
        s=0;
        scanf("%lld",&n);
        struct node*head,*p;
        head=(struct node*)malloc(sizeof(struct node));
        head->next=NULL;
        head->upward=NULL;
        tail=head;
        for(int i=1; i<=n; i++)
        {
            p=(struct node*)malloc(sizeof(struct node));
            p->data=1.0/i;
            p->num=i;
            p->next=NULL;
            if(i%2==0)p->fore='-';
            else p->fore='+';
            p->upward=tail;
            tail->next=p;
            tail=p;
        }
        printf("%.2lf\n",sum(tail));
    }
   return 0;
}
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
    char fore;
    double data;
    struct node*next;
};
double create(int n)
{
    struct node*head,*tail,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    double s=0;
    if(n<=60)
    {
        for(int i=1; i<=n; i++)
        {
            p=(struct node*)malloc(sizeof(struct node));
            p->data=1.0/i;
            tail->next=p;
            tail=p;
            if(i%2==0)
            {
                p->fore='-';
                s-=p->data;
            }
            else
            {
                p->fore='+';
                s+=p->data;
            }
        }
    }
    else if(n>=61&&n<=269)
    {
        if(n%2!=0)
        {
            s=0.70;
        }
        else s=0.69;
    }
    else s=0.69;
    return s;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%.2lf\n",create(n));
    }
    return 0;
}
//这是个交错级数,肯定收敛,所以一定有规律,那样暴力求和肯定会超时
//这个题就是考找规律,写出前100个数就能发现规律,然后再多打印
//会发现后面的数都收敛了,直接写出规律即可

猜你喜欢

转载自blog.csdn.net/bhliuhan/article/details/81154473