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 ;
}

最新文章

  1. selenium使用Xpath定位之完整篇
  2. js通过注册表找到本地软件安装路径并且执行
  3. C#读取大文本文件
  4. KANO模型
  5. MySQL的Sleep进程
  6. html2canvas手机端模糊问题
  7. Native libraries .so.XY failing to link at runtime
  8. 关于安装PHP补装PDO与PDO_MYSQL操作
  9. Node.js、Ionic、Cordova、AngualrJS安装
  10. Sum of AP series——AP系列之和
  11. zabbix监控ssl证书到期时间
  12. Netcat实用操作
  13. AE与C#入门笔记
  14. Latex数学公式中的空格
  15. ssh远程免密登录Linux
  16. 【BZOJ-4530】大融合 线段树合并
  17. MSDN离线版 发现不少人都在找这个
  18. 1 salt执行模块开发
  19. mac os 里的 JAVA_HOME
  20. pkg-config 切换opencv版本

热门文章

  1. DataTable中的select()用法
  2. subset子集全排序问题
  3. C# 多态(2)
  4. 「CF622F」The Sum of the k-th Powers「拉格朗日插值」
  5. spark-2.2.1在centos7安装
  6. Python之路番外:PYTHON基本数据类型和小知识点
  7. adb命令connect设备必须添加端口号
  8. [jvm]基于jvm的线程实现
  9. 转载Java NIO中的Files类的使用
  10. [FJOI2017]矩阵填数