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. git vs sourcetree

    SourceTree&Git部分名词解释 克隆(clone):从远程仓库URL加载创建一个与远程仓库一样的本地仓库 提交(commit):将暂存文件上传到本地仓库(我们在Finder中对本地仓 ...

  2. vs2015 使用 Eazfuscator.NET 3.3

    出现问题: Unable to cast object System.Xml.XmlComment’ to type ‘System.Xml.XmlElement’ 解决办法: 打开 *.csproj ...

  3. Struts2学习第3天--OGNL、EL、值栈

    JAVA中的OGNL: 1 调用对象的方法: 2 访问对象的静态方法: 3 获取OGNLContext.Root中的数据. User: 4 访问Context: 关键还是在Struts2环境中的使用: ...

  4. memcached装、启动和卸载

    1.下载相关软件: 下载地址:http://download.csdn.net/download/wangshuxuncom/8249501: 2.解压获取到的压缩文件,将得到一个名为“memcach ...

  5. 解决 Github用户名 变为 invalid-email-address 问题

    解决 Github用户名 变为 invalid-email-address 问题 If the identity used for this commit is wrong, you can fix ...

  6. 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

    数据结构实验之图论七:驴友计划 Time Limit: 1000 ms           Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  7. minizip -基于zlib开源代码库

    转载:https://www.topomel.com/archives/979.html 一.minizip 是一套用来压缩和解压文件的工具,其基于zlib开源代码库. 开源代码下载链接:http:/ ...

  8. How to: Create a Business Model in the XPO Data Model Designer

    How to: Create a Business Model in the XPO Data Model Designer This topic provides step-by-step inst ...

  9. Qt 学习之路 2(37):文本文件读写

    Qt 学习之路 2(37):文本文件读写 豆子 2013年1月7日 Qt 学习之路 2 23条评论 上一章我们介绍了有关二进制文件的读写.二进制文件比较小巧,却不是人可读的格式.而文本文件是一种人可读 ...

  10. 【转】idea新建项目文件名为红色的解决办法

    idea如果当前project用了版本控制器,其下面新建的所有的项目默认都是加入到版本控制里面,所以项目名称和文件都是红色的. 解决办法: ctrl + alt + s 进入设置界面,–>ver ...