PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
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 (,) are the exponents and coefficients, respectively. It is given that 1, 0.
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct poly{
int expo;
double coef;
};
void add(vector<poly>& vec,poly po){
for(int i=;i<vec.size();i++){
if(vec[i].expo==po.expo){
vec[i].coef+=po.coef;
if(vec[i].coef==){
vec.erase(vec.begin()+i);
/**这边需要判断0多项式,进行erase掉*/
}
return;
}
}
if(po.coef!=) vec.push_back(po);
}
bool cmp(poly p,poly p2){
return p.expo>p2.expo;
}
int main(){
/**
* 注意点
* 1.保留一位小数
*/
int M,N;vector<poly> res;
cin>>M;
poly p[M];
for(int i=;i<M;i++){
cin>>p[i].expo>>p[i].coef;
}
cin>>N;
poly p2[N];
for(int i=;i<N;i++){
cin>>p2[i].expo>>p2[i].coef;
}
for(int i=;i<M;i++){
for(int j=;j<N;j++){
poly temp;
temp.expo=p[i].expo+p2[j].expo;
temp.coef=p[i].coef*p2[j].coef;
add(res,temp);
}
}
/**这边需要sort一下*/
sort(res.begin(),res.end(),cmp);
cout<<res.size();
for(int i=;i<res.size();i++){
printf(" %d %.1f",res[i].expo,res[i].coef);
}
system("pause");
return ;
}
需要记住vector进行删除元素,用的是erase(iter*)
PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)的更多相关文章
- 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)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
- 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) 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)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 【PAT甲级】1009 Product of Polynomials (25 分)
题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...
- 【PAT】1009. Product of Polynomials (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...
- 1009 Product of Polynomials (25分) 晚上脑子就是容易僵住
#include<iostream> using namespace std; struct { int a; double b; }poly[1001]; double a[2001]; ...
- PATA 1009. Product of Polynomials (25)
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
随机推荐
- 实现mypwd(选做)
实现mypwd(选做) 任务清单 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd (一)pwd命令的学习 1.pw ...
- a = a + b 与 a += b 的区别
1.对于同样类型的a,b来说 两个式子执行的结果确实没有什么区别.但是从编译的角度看吧(武让说的),a+=b;执行的时候效率高. 2.对于不同类型的a,b来说 2.1 不同类型的两个变量在进行运 ...
- ACL 2019 分析
ACL 2019 分析 word embedding 22篇! Towards Unsupervised Text Classification Leveraging Experts and Word ...
- C++ STL 关于双向链表list的splice函数
转载自https://blog.csdn.net/qjh5606/article/details/85881680 list::splice实现list拼接的功能.将源list的内容部分或全部元素删除 ...
- 自定义标记mark
前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是i ...
- LoadRunner之关联
一.什么是关联 关联就是将服务器动态返回变化的值保存为一个参数以供后面需要用到的地方使用. 二.什么时候需要关联 1.服务器返回中存在动态变化的值,一般是类似session.token这样的无规则数据 ...
- vue 中滚动div至最底部
scrollToBottom: function () { this.$nextTick(() => { var container = this.$el.querySelector(" ...
- database 学习
ref : 什么是NoSQL,为什么要使用NoSQL?
- Python示例-Logging
logging.ini日志配置文件内容示例: [loggers] keys=root,demo [handlers] keys=consoleHandler,timedRotatingFileHand ...
- 加载动态链接库——dlopen dlsym dlclose
DLOPEN DLMOPEN DLCLOSE NAME dlclose, dlopen, dlmopen - 打开/关闭共享对象 SYNOPSIS #include <dlfcn.h&g ...