多项式求和 链表
来源:互联网 发布:垂直同步 知乎 编辑:程序博客网 时间:2024/06/08 06:05
此题有一个坑,就是因为数太大会超时,但试几组数据就会发现,当它到一定的数时,结果就不变了,可以缩小计算范围
多项式求和
Time Limit: 1000MS Memory limit: 65536K
题目描述
多项式描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 ……
先请你求出多项式前n项的和。
输入
第一行输入一个数T代表测试数据个数(T<=1000)。接下来T行每行1个数代表n(0<=n< 2^31)。
输出
对于每个输入样例,输出多项式和的结果(结果精确到小数点后两位)。每行输出一个结果。
示例输入
212
示例输出
1.00
0.50
#include <stdio.h>#include <stdlib.h>#include<math.h>using namespace std;struct node{ double data; struct node *next;};int main(){ int a; int n,i; struct node *head,*tail,*p; scanf("%d",&a); while(a--) { scanf("%d",&n); if(n>=200)//*******************注意 n=200; head=(struct node *)malloc (sizeof(struct node)); head->next=NULL; tail=head; for(i=1; i<=n; i++) { p=(struct node *)malloc(sizeof(struct node)); p->data=pow(-1,i+1)*(1/(double)i); p->next=NULL; tail->next=p; tail=p; } double sum=0; p=head->next; while(p) { sum=sum+p->data; p=p->next; } printf("%.2lf\n",sum); }}
0 0
- 链表,多项式求和
- 多项式求和 链表
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 求矩阵的最大和路径问题
- web框架学习前复习——html
- Maven2的配置文件settings.xml
- Java 堆内存和桟内存的详解
- uboot移植之网络驱动移植--移植操作--7.29
- 多项式求和 链表
- CentOS 7.0系统安装配置图文教程
- Android开发方向
- replace indexOf 正则表达式
- VS2013
- php在windows2008运行的问题
- CentOS 7.0 安装配置LAMP服务器方法(Apache+PHP+MariaDB)
- 304. Range Sum Query 2D - Immutable
- Spring注解实现Bean定义