多项式求和
来源:互联网 发布:ubuntu卸载搜狗输入法 编辑:程序博客网 时间:2024/06/06 02:58
多项式求和
Time Limit: 1000MS Memory Limit: 65536KB
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
对于每个输入样例,输出多项式和的结果(结果精确到小数点后两位)。每行输出一个结果。
Example Input
212
Example Output
1.000.50
Hint
Author
中国海洋大学第三届“朗讯杯”编程比赛高级组试题
//一般的解法
#include<iostream>#include<cstdio>#include<iomanip>using namespace std;int main(){ int n,m; scanf("%d",&n);while(n--){scanf("%d",&m);if(m>270) //???printf("0.69\n");else{ double sum=0.0; for(int i=1;i<=m;i++) { if(i%2) sum+=1.0/i; else sum-=1.0/i; } printf("%.2f\n",sum);}}return 0;}
//用链表求解
//多项式求和#include<iostream>#include<cstdio>#include<iomanip>using namespace std;struct node{ double data;node *next;};node *head;void Init(){head=new node;head->data=0; head->next=NULL;}void Creat(node *&head){ node *q=new node;q=head;for(int i=1;i<=270;i++){node *p=new node; if(i%2) p->data=(q->data+1.0/i);elsep->data=(q->data-1.0/i);p->next=NULL;q->next=p;q=p;}}void print(node *&head,int m){ node *q=new node; q=head; while(m--) q=q->next; printf("%0.2f\n",q->data); }int main(){ int n,m; scanf("%d",&n);while(n--){ scanf("%d",&m);if(m>270) //为什么加上这个就AC了呢?不加是TLEprintf("0.69\n");else{Init(); Creat(head); print(head,m);}}return 0;}
阅读全文
0 0
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- 多项式求和
- Integer.getInteger()是逗比
- [noip2013]转圈游戏
- 鼠标悬停时,使用xpath定位页面按钮元素
- HDU 6168 Numbers
- 学习笔记整理2——rxjava和Retrofit摘录
- 多项式求和
- SAP Cloud Fiori News Tile的配置
- Java开发必装的IntelliJ IDEA插件
- 前端代码埋点
- HDU --- 5963 朋友 【树上博弈】
- Windows下安装Maven并引用aliyun镜像服务器
- 瑞士轮
- request.setAttribute和request.getAttribute还有session.setAttribute和session.getAttribute还有request.getPar
- MapReduce找出访问量最大的表