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 ...
随机推荐
- 使用私有仓库(Docker Registry 2.0)管理镜像
1. 执行以下命令新建并启动一个Docker Registry 2.0 docker run -d -p 5000:5000 --restart=always --name registry2 reg ...
- [LeetCode]-DataBase-Duplicate Emails
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- 配置Nginx和Apache允许指定域名CORS跨域访问
前后端分离开发,导致前端项目需要跨域请求后端接口,解决方法有很多,本文只介绍两个: 1. 修改后端程序代码实现允许跨域请求 2. 修改服务器配置文件实现允许跨域请求 正文: 方法1:修改后端程序代码实 ...
- Springboot 后台管理框架halo.kotlin
https://gitee.com/backControl/halo-kotlin 现在开了两个分支,一个分支在做数据分析,一个分支是集成spring oauth2.0技术 之后会接入最新的技术和实现 ...
- Windows下启动.Net Core程序脚本
@echo offstart cmd /k "cd /D %~dp0&&dotnet xxx.dll" cmd /k 是执行完dir命令后不关闭命令窗口 cd /d ...
- LinkedList Stack
- 数据结构和算法(Java版)快速学习(栈与队列)
栈是仅允许在表尾进行插入和删除操作的线性表.我们把允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom).栈是一种后进先出(Last In First Out)的线性表,简称(LIFO ...
- 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...
- 11 ORA-8102:Index Corruption解析
11 ORA-8102:Index Corruption解析 [oracle@DSI ~]$ oerr ora 810208102, 00000, "index key not found, ...
- 安装golang web框架 gin
gin 地址https://github.com/gin-gonic/gin#installation 去gin 地址 clone 下来,放到对应的包中即可.如:gin就放在项目文件夹/github. ...