pat1009. Product of Polynomials (25)
1009. Product of Polynomials (25)
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)的更多相关文章
- PAT1009:Product of Polynomials
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- 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 ...
- pat 甲级 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PATA 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Spring中的用到的设计模式
应该说设计模式是我们在写代码时候的一种被承认的较好的模式.好的设计模式就像是给代码造了一个很好的骨架,在这个骨架里,你可以知道心在哪里,肺在哪里,因为大多数人都认识这样的骨架,就有了很好的传播性.这是 ...
- 正则表达式回溯-导致CPU偏高
最近了解了下有关正则表达式回溯的内容,想想就写下来,方便自己. 正则表达式匹配算法是建立在正则表达式引擎的基础上的,目前有两种引擎:DFA(确定型有穷自动机)和NFA(不确定型有穷自动机).这两种引擎 ...
- Ext.Net安装和应用
1.最新版本 2.打开文件,将包含以下文件: Ext.Net.dll Ext.Net.Utilities.dll Ext.Net.xml Newtonsoft.Json.dll Newto ...
- linux chmod对文件权限的操作
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读.写.运行设定权限. 例如我用ls -l命令列文件表时,得到如下输出: -rw-r--r-- 1 apple users ...
- day7学python 初识简单模板
初识简单模板 模块与包 1.模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现功能),本质是.py结尾的文件 但导入的模块名,无.py 2.包:从逻辑上组织模块,本质就是目录(含有_i ...
- OOP2(虚函数/抽象基类/访问控制与继承)
通常情况下,如果我们不适用某个函数,则无需为该函数提供定义.但我们必须为每个虚函数都提供定义而不管它是否被用到了,这因为连编译器也无法确定到底会适用哪个虚函数 对虚函数的调用可能在运行时才被解析: 当 ...
- django-redis 使用规范
django-redis 基于 BSD 许可, 是一个使 Django 支持 Redis cache/session 后端的全功能组件. 1,安装 django-redis 最简单的方法就是用 pip ...
- 【bzoj4176】Lucas的数论 莫比乌斯反演+杜教筛
Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么喜欢了. 在整理以前的试题时,发现了这样一道题目"求Sigma(f(i)),其中1<=i< ...
- 从HDU1004来看C++<map>
最近做题有点浮躁~于是从HDU第一题开始刷,刷到1004题的时候突发奇想来复习一发很久没有用到过的map 题意是给你n个气球,每个气球有一种颜色,让你求出数量最多的气球颜色 因为颜色是气球数量的识别关 ...
- MyBatis配置文件的配置说明
Properties 1.创建一个资源文件jdbc.properties: jdbc.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.url= ...