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 ...
随机推荐
- 关于判断StringBuffer是否为空
对于String和StringBuffer来说,都是通过创建新的char value[]数组来达到字符串改变的操作的,只不过String是通过新创建String对象来达到目的, 而StringBuff ...
- mysql 数据增删改的总结
一.在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括 1.使用INSERT实现数据的插入2.UPDATE实现数据的更新3.使用DELETE实现数据的删除4.使用SELEC ...
- @清晰掉 c语言三"巨头" const:volatile:static
const: 1.如果把const放在变量类型前,说明这个变量的值是保持不变的(即为常量),改变量必须在定义时初始化,初始化后对她的任何赋值都是非法的. 2.当指针或是引用指向一个常量时,必须在类型名 ...
- taihong
揭秘!除了台风 我们还能在卫星云图上看到什么? 2019-08-08 09:20:53 来源: 中国天气网 中国天气网讯 说到卫星云图,可能大多数人首先能想到的就是台风.其实,拥有太空视角的卫星能 ...
- windows 把ps/2 鼠标当成ps/2键盘了
真坑口阿 https://zhidao.baidu.com/question/425134865713508932.html 电脑的PS/2鼠标接口认成键盘了 电脑主板技嘉,只有一个PS/2接口.开始 ...
- 安装golang web框架 gin
gin 地址https://github.com/gin-gonic/gin#installation 去gin 地址 clone 下来,放到对应的包中即可.如:gin就放在项目文件夹/github. ...
- maven scope provided和runtime的例子
maven常用的scope有compile,provided,runtime,test. complie是默认值,表示在build,test,runtime阶段的classpath下都有依赖关系.te ...
- 第十届山东省acm省赛补题(1)
今天第一场个人训练赛的题目有点恐怖啊,我看了半个小时多硬是一道都不会写.我干脆就直接补题去了.... 先补的都是简单题,难题等我这周末慢慢来吧... A Calandar Time Limit: 1 ...
- VS2017使用dotnet命令
添加引用Microsoft.EntityFrameworkCore.Tools 添加引用后提示未找到命令“dotnet ef”向csprog文件添加如下节点 <ItemGroup> < ...
- linux上执行mysql的脚本文件
我们测试过程中,经常需要执行升级脚本或导入生产测试数据,对于轻量的升级脚本可以直接在客户端工具中打开执行,但是对于文件内容比较大的.sql文件,比如几百M,几G的sql文件,直接拖到客户端工具打开执行 ...