多项式求和 链表
2016-07-29 22:59
381 查看
此题有一个坑,就是因为数太大会超时,但试几组数据就会发现,当它到一定的数时,结果就不变了,可以缩小计算范围
多项式求和
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 ……
先请你求出多项式前n项的和。
0.50
多项式求和
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)。输出
对于每个输入样例,输出多项式和的结果(结果精确到小数点后两位)。每行输出一个结果。示例输入
2 1 2
示例输出
1.000.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); } }
相关文章推荐
- 如何组织构建多文件 C 语言程序(二)
- [C/C++]反转链表
- 如何写好 C main 函数
- C#数据结构之顺序表(SeqList)实例详解
- C#实现基于链表的内存记事本实例
- Lua和C语言的交互详解
- Lua教程(七):数据结构详解
- C#模拟链表数据结构的实例解析
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C语言编程中统计输入的行数以及单词个数的方法
- C 语言简单加减乘除运算