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 ...
随机推荐
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 6.
1. Introduction. 1.1 Starting from part 4 I have started to discuss how to interop marshal a managed ...
- rsync实时备份备份服务搭建和使用指南
一.Rsync企业工作场景说明: 1.利用定时任务+rsync方式实现数据同步 对于网站内部技术人员创建的数据,可以采取定时任务的方式 2.利用实时任务+rsync方式实现数据同步 对于网站外部访问用 ...
- MVN package install error javac: invalid target release: 1.8
现象:---------------------------------[ERROR] Failure executing javac, but could not parse the error:j ...
- 基于redis的分布式锁的分析与实践
前言:在分布式环境中,我们经常使用锁来进行并发控制,锁可分为乐观锁和悲观锁,基于数据库版本戳的实现是乐观锁,基于redis或zookeeper的实现可认为是悲观锁了.乐观锁和悲观锁最根本的区别在于 ...
- centos6.5 git clone http 报错
自己搭建服务器环境为centos6.5,需要使用git clone 命令的时候报错 首先查看centos上安装的git版本,我的版本为1.7.10 报错后,查阅相关资料需将centos升级,操作如下 ...
- 【python】10分钟教你用python一行代码搞点大新闻
准备 相信各位对python的语言简洁已经深有领会了.那么,今天就带大家一探究竟.看看一行python代码究竟能干些什么大新闻.赶紧抄起手中的家伙,跟我来试试吧. 首先你得先在命令行进入python. ...
- (原創) Gvim 個人習慣常用設定 (vim)
不定期更新這篇,因為查詢到好用的設定或者插件就會更新自己的設定. "set nocompatible let $LANG='zh_TW.UTF-8' set langmenu=zh_tw.u ...
- ajax返回数据成功 却进入error方法
应该是dataType的原因,dataType为json,但是返回的data不是json格式 于是将dataType:"json"去掉就ok了
- 小程序tabBar显示问题
我一直在纠结为什么小程序在有的页面显示,有的页面不显示 查了一下才知道,小程序tabBar只在tabBar中的list定义了页面的才会显示,其余页面不会显示 如下例:tabBar只在画红框的页面显示
- 用qt creator创建可继承ui类
https://jingyan.baidu.com/article/5d368d1efa2dd73f60c05786.html 用qt creator创建可继承ui类 听语音 | 浏览:1657 | ...