CCCC L2-018. 多项式A除以B 直接上map,然后stack处理输出
https://www.patest.cn/contests/gplt/L2-018
题意:模拟多项式除法。
题解:短除法,初中奥数老师,高中数学老师,高数老师都讲过2333。
模拟之前,关于保存 多项式的方法。考虑到系数可能很分散(1~1e9),直接用map存(map基本就是为多项式加减法定制的)
模拟的时候,就是数学课上短除法的算法,注意一下循环终止条件及其控制:当多项式A的最高次系数小于B的时,除不下去,此时A已然为余数。
模拟完,输出 。本来用stack只是因为auto:t不能逆向输出,结果顺便把四舍五入及删零操作顺便解决了(不理解的地方是floor(x+0.5)对负数(-0.5)为什么也能四舍五入,貌似数据太弱)。
最后,记得判掉零项
坑:题目没看清,没有特判掉零多项式,然后在那里XJBL改。//改了负数的四舍五入,改了一下及时erase掉系数为0的项,显然并卵。无奈搜了一下题解,ac代码赫然多了一行 “0 0 0” 0_0!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include<stack>
#include<set>
#include<string.h>
#include<list>
#define pb push_back
#define mp make_pair
#define _for(i, a, b) for (int i = (a); i<(b); ++i)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int len;
map<int, float> a, b, c, d;
int main() {
int n; cin >> n;
_for(i, 0, n) {
int x, y;
cin >> x >> y;
a[x] = y;
}
cin >> n;
_for(i, 0, n) {
int x, y;
cin >> x >> y;
b[x] = y;
}
//模拟短除法:
while (a.rbegin()->first >= b.rbegin()->first) {
int e = a.rbegin()->first - b.rbegin()->first;
float f = a.rbegin()->second / b.rbegin()->second;
c[e] += f;
for (auto t : b) {
a[t.first + e] -= f*t.second;
}
if (a.rbegin()->second == 0) a.erase(a.rbegin()->first);
}
//反向输出,四舍五入判定
stack<pair<int, float> > ta, tb;
for (auto t : c) {
float temp = t.second * 10;
temp = floor(temp + 0.5);
if (temp == 0) continue;
else {
temp /= 10;
ta.push(mp(t.first, temp));
}
}
for (auto t : a) {
float temp = t.second * 10;
temp = floor(temp + 0.5);
if (temp == 0) continue;
else {
temp /= 10;
tb.push(mp(t.first, temp));
}
}
cout << ta.size(); if (ta.size() == 0) { cout << " 0 0.0"; }else while (!ta.empty()) { cout << ' ' << ta.top().first << ' '; printf("%.1lf", ta.top().second); ta.pop(); }
cout << endl;
cout << tb.size(); if (tb.size() == 0) {cout << " 0 0.0" ;}else while (!tb.empty()) { cout << ' ' << tb.top().first << ' '; printf("%.1lf", tb.top().second); tb.pop(); }
system("pause");
}
/*1 2 3*/
CCCC L2-018. 多项式A除以B 直接上map,然后stack处理输出的更多相关文章
- 多项式A除以B
这个问题我是在PAT大区赛题里遇见的.题目如下: 多项式A除以B(25 分) 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数 ...
- L2-018. 多项式A除以B*
L2-018. 多项式A除以B 参考博客 #include <iostream> #include <map> #include <cmath> #include ...
- (转载) 天梯赛 L2-018. 多项式A除以B
题目链接 题目描述 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出 ...
- 7-10 多项式A除以B (25分)(多项式除法)
7-10 多项式A除以B (25分) 这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出 ...
- PAT L2-018. 多项式A除以B
暴力,模拟. 比赛搞了一个小时搞到了$1$分.赛场上不够冷静......之前没接触过多项式除法,但赛场上想到了除法的规则,莫名其妙写的时候不知道哪里崩了.对于这样的题目,应该先测一测数据的指数是不是很 ...
- 团体程序设计天梯赛 L2-018. 多项式A除以B(模拟)
题意:给你A,B两个多项式,问你A/B的值:注意多项式给你的是每个式子的指数与系数:保留到一位小数,如果出现系数为0(保留后也是)的情况,请不要输出它,如果没有非系数为0的情况就输出特殊 题解:多项式 ...
- L2-018 多项式A除以B(模拟)
这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出A,再给出B.每行的 ...
- 7-10 多项式A除以B (25 分)
题目链接:https://pintia.cn/problem-sets/1108548596745592832/problems/1108548661014913033 题目大意: 这仍然是一道关于A ...
- pta l2-18(多项式A除以B)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984 题意:给定两个多项式,求出其做除法 ...
随机推荐
- C#------如何使用Swagger调试接口
1.打开NuGet程序包 2.安装下面两个程序包 3.安装完后会出现SwaggerConfig.cs类,并修改里面的内容 代码: [assembly: PreApplicationStartMetho ...
- Android中显示照片的Exif信息
package com.hyzhou.pngexifdemo; import android.media.ExifInterface; import android.os.Bundle; import ...
- 【GIS】Cesium GLTF
cd D:\GISSoft\3DsMax2017\COLLADA2GLTF-v2.1.4-windows-Release-x64 COLLADA2GLTF-bin.exe -f tree05.DAE ...
- RGB24转YUV420
BOOL RGB2YUV(LPBYTE RgbBuf, UINT nWidth, UINT nHeight, LPBYTE yuvBuf, unsigned long *len) { if (RgbB ...
- apt-get/dpkg常用指令备查
apt-get install <package> Downloads <package> and all of its dependencies, and installs ...
- Selenium 获取节点信息
Selenium 可以通过 find_element() 找到指定的节点,Selenium 也提供了相关的方法和属性来直接提取节点信息,如属性.文本等 from selenium import web ...
- Ansible常用模块使用
Ansible官方提供了非常多的模块,还有若干第三方模块,我们也可以自己编写模块. Ansible对远程服务器的操作实际是通过模块完成的,先将模块拷贝到远程服务器,完成操作后,然后在远程服务器上删除该 ...
- 手机CPU
说起手机CPU的历史,笔者给大家提一个问题:"世界上第一款智能手机是什么呢?"相信很多人的答案是爱立信的R380或诺基亚的7650,但都不对,真正的首款智能手机是由摩托罗拉在200 ...
- ip防刷脚本
#!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...
- 【LeetCode OJ】Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return ...