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. windows phone开发必备工具翔

    1.图标设计 http://www.flaticon.com/ http://www.iconfont.cn/ 2.界面设计: 2.1.behance.com    2.2.dribbble.com  ...

  2. 一个基于 .NET Core 2.0 开发的简单易用的快速开发框架 - LinFx

    LinFx 一个基于 .NET Core 2.0 开发的简单易用的快速开发框架,遵循领域驱动设计(DDD)规范约束,提供实现事件驱动.事件回溯.响应式等特性的基础设施.让开发者享受到正真意义的面向对象 ...

  3. Persistent and Transient Data Structures in Clojure

    此文已由作者张佃鹏授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在项目中用到了Transient数据结构,使用该数据结构对程序执行效率会有一定的提高.刚刚接触Trans ...

  4. [Algorithm]图

    一.图的算法 邻接矩阵表示的数据结构 1 #define INFINITY INT_MAX // 无穷大 2 #define MAX_VERTEX_NUM 20 // 限制顶点最大数值为20个 3 # ...

  5. 201621123012 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  6. NSCache 缓存

    前言 NSCache 是苹果提供的一个专门用来做缓存的类,当内存 "不足" 或超过限制的时候,会自动清理缓存,使用时可以指定缓存的数量和成本. 用法与 NSMutableDicti ...

  7. B - N皇后问题

    原文链接 一天课下,张老板研究起了国际象棋,渴望完美的他更改了棋盘的大小,在N*N的方格棋盘放置了N个皇后,希望它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的 ...

  8. 洛谷P4517 [JSOI2018]防御网络(dp)

    题面 传送门 题解 翻译一下题意就是每次选出一些点,要用最少的边把这些点连起来,求期望边数 我也不知道为什么反正总之就是暴力枚举太麻烦了所以我们考虑贡献 如果一条边是割边,那么它会在图里当且仅当两边的 ...

  9. postgreSQL PL/SQL编程学习笔记(六)——杂七杂八

    1 PL/pgSQL Under the Hood This part discusses some implementation details that are frequently import ...

  10. eclipse创建Java项目时提示Open Associated Perspective?

    在eclipse中,原先使用python进行编程,需要新建java项目时,会提示如下信息: 消息框内翻译如下: Open Associated Perspective? --开放关联视角? This ...