1009. Product of Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

This time, you are supposed to find A*B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 3 3.6 2 6.0 1 1.6

提交代码

当觉得主要的算法没有问题时,花点时间去关注一些关键变量的变化规律,比如这里的num。

 #include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
#include <cmath>
using namespace std;
#define exp 1e-9
struct node{
int e;
double c;
node *next;
};
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
node *a,*b,*head;
head=new node();
head->next=NULL;
int na;
scanf("%d",&na);
int i;
a=new node[na];
for(i=;i<na;i++){
scanf("%d %lf",&a[i].e,&a[i].c);
}
int nb;
scanf("%d",&nb);
b=new node[nb];
for(i=;i<nb;i++){
scanf("%d %lf",&b[i].e,&b[i].c);
}
int j,num=;
for(i=;i<na;i++){
for(j=;j<nb;j++){
int e=a[i].e+b[j].e;
double c=a[i].c*b[j].c;
node *q=head,*t=head->next;
while(t){
if(t->e<e){//q->e>=e
break;
}
q=t;
t=q->next;
}
if(q!=head&&q->e==e){
q->c+=c;
}
else{
t=new node();
t->c=c;
t->e=e;
t->next=q->next;
q->next=t;
}
}
}
node *p;
p=head->next;
while(p){//系数可能为0!! 这里注意!!
if(abs(p->c)>exp)
num++;
p=p->next;
}
p=head->next;
printf("%d",num);
while(p){
if(abs(p->c)>exp)//系数可能为0!!
printf(" %d %.1lf",p->e,p->c);
head->next=p->next;
delete p;
p=head->next;
}
printf("\n");
delete []a;
delete []b;
delete head;
return ;
}

pat1009. Product of Polynomials (25)的更多相关文章

  1. PAT1009:Product of Polynomials

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. PAT甲 1009. Product of Polynomials (25) 2016-09-09 23:02 96人阅读 评论(0) 收藏

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. PAT 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)

    1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...

  4. A1009 Product of Polynomials (25)(25 分)

    A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...

  5. pat 甲级 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. PATA 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  8. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

  9. PAT 解题报告 1009. Product of Polynomials (25)

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

随机推荐

  1. Oracle动态执行表不可访问

    在scott 用户下,执行查询语句是出现"Oracle动态执行表不可访问" 经查,是因为用户权限不够所致,修改scott用户权限语句如下: grant select on V_$s ...

  2. .Net Core 项目引用本地类库方式(一)

    最近了解到.NET Core 项目,引用本地类库DLL的方式有三种 1.非同解决方案下的引用,直接引用,浏览,找到对应的DLL,然后确定引用. 这种方式有个不好的地方就是,如果引用的DLL文件里面,也 ...

  3. javascript 视频播放指定的时间段

    javascript 视频播放指定的时间段 一.html5 vedio: //指定开始时间 player.currentTime=startPoint; player.play(); //使用事件来控 ...

  4. Delphi XE8中开发DataSnap程序常见问题和解决方法 (-)启动创建好的DBExpress工程时候报错了!

    当我们成功创建了使用DBExpress的DataSnap的服务器和客户端程序后,我们关闭了当前工程,当我们再次打开时候,有可能会出现这样的问题: 问题原因:这个问题是因为当前工程组默认启动的是客户端工 ...

  5. 单链表倒数第K个节点的查找和显示

    1.使用一个固定长度队列装链表段,当遍历到链表根时,返回队列头元素. class Node{ int value; Node next; public Node(int value){ this.va ...

  6. javascript立即执行函数与模块化

    概念:立即执行函数顾名思义就是函数定义好之后立即执行.函数表达式方式:函数表达式后面加括号()即可立即执行函数. var xmlhttpUtil = function () { function ge ...

  7. node.js安装以及git 的使用说明

     第一步:安装node.js: Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.   Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高 ...

  8. Python实现——一元线性回归(梯度下降法)

    2019/3/25 一元线性回归--梯度下降/最小二乘法_又名:一两位小数点的悲剧_ 感觉这个才是真正的重头戏,毕竟前两者都是更倾向于直接使用公式,而不是让计算机一步步去接近真相,而这个梯度下降就不一 ...

  9. win10系统常用操作

    1.打开控制面板 (1)选中“此电脑”,右键选择“属性”. (2)可见 控制面板主页 2.设置环境变量和系统变量 (1)选中“此电脑”,右键选择“属性”. (2)可见 控制面板主页 (3)点击高级系统 ...

  10. R语言学习笔记(三)

    5. 数据结构 5.1 数据结构简介 (1)向量 一个向量的所有元素必须有相同的类型(模式) (2)列表 列表可以非同质的 列表可按位置索引:lst[[2]] 抽取子列表:lst[c(2,5)] 列表 ...