标题
一元稀疏多项式计算器
时间限制
2S
内存限制
10000 Kb
问题描述
设计一个一元稀疏多项式的简单计算器,要求能进行加减运算。
问题输入
每组数据有3行构成,第1行为3个正整数n,m,t, n表示第一个多项式的项数,m表示第二个多项式的项数,t表示运算类型,0为加法,1为减法,每组数据的第2行包含2n个整数,每两个整数分别表示第一个多项式每一项的系数和指数;第3行包含2m个整数,每两个整数分别表示第二个多项式每一项的系数和指数。输入每一项按指数从低到高的顺序排列。
问题输出
在一行上以多项式形式输出结果,指数按从低到高的顺序
输入样例
6 3 0
1 0 1 1 -3 2 1 3 1 4 1 5
-1 3 -2 4 1 5
输出样例
1+x-3x^2-x^4+2x^5
1.本题是 Felix 第一次用 vscode 自动代码补全写的代码,可以说 Copilot 真的很好用,定义一个变量, if 一下, 所有代码都提示出来了;
2.80 分的原因可能是没有考虑最终相减为 0 的情况
3.注意迭代器不能在遍历时 erase 删除
// 2024/12/30 OK with the help of Copilot
#include <bits/stdc++.h>
using namespace std;
int n, m, t;
map<int, int> mp1, mp2;
map<int, int> Addition(map<int, int> mp1, map<int, int> mp2)
{
map<int, int> res;
for (auto it = mp1.begin(); it != mp1.end(); it ++) {
res[it->first] = it->second;
}
for (auto it = mp2.begin(); it != mp2.end(); it ++) {
if (res.find(it->first) != res.end()) {
res[it->first] += it->second;
} else {
res[it->first] = it->second;
}
}
return res;
}
map<int, int> Subtraction(map<int, int> mp1, map<int, int> mp2)
{
map<int, int> res;
for (auto it = mp1.begin(); it != mp1.end(); it ++) {
res[it->first] = it->second;
}
for (auto it = mp2.begin(); it != mp2.end(); it ++) {
if (res.find(it->first) != res.end()) {
res[it->first] -= it->second;
} else {
res[it->first] = -it->second;
}
}
return res;
}
int main()
{
cin >> n >> m >> t;
for (int i = 1; i <= n; i ++) {
int a, b;
cin >> a >> b;
mp1[b] = a;
}
for (int i = 1; i <= m; i ++) {
int a, b;
cin >> a >> b;
mp2[b] = a;
}
map<int, int> res;
if (!t) {
res = Addition(mp1, mp2);
} else {
res = Subtraction(mp1, mp2);
}
int f = 0;
for (auto it = res.begin(); it != res.end(); it ++) {
if (it->second != 0) {
f = 1;
break;
}
}
if (!f) {
cout << "0";
return 0;
}
int flag = 0;
for (auto it = res.begin(); it != res.end(); it ++) {
if (it->second == 0) {
continue;
}
if (flag == 0 && it->second < 0) {
cout << "-";
} else if (flag != 0 && it->second > 0) {
cout << "+";
}
if (it->first == 0) {
cout << it->second;
} else if (it->first == 1) {
if (it->second == 1) {
cout << "x";
} else if (it->second == -1) {
cout << "-x";
} else {
cout << it->second << "x";
}
} else {
if (it->second == 1) {
cout << "x^" << it->first;
} else if (it->second == -1) {
cout << "-x^" << it->first;
} else {
cout << it->second << "x^" << it->first;
}
}
flag = 1;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n, m, t;
map<int, int> mp1, mp2;
map<int, int> Addition(map<int, int> mp1, map<int, int> mp2)
{
map<int, int> res;
for (auto it = mp1.begin(); it != mp1.end(); it ++) {
res[it->first] = it->second;
}
for (auto it = mp2.begin(); it != mp2.end(); it ++) {
if (res.find(it->first) != res.end()) {
res[it->first] += it->second;
} else {
res[it->first] = it->second;
}
}
return res;
}
map<int, int> Subtraction(map<int, int> mp1, map<int, int> mp2)
{
map<int, int> res;
for (auto it = mp1.begin(); it != mp1.end(); it ++) {
res[it->first] = it->second;
}
for (auto it = mp2.begin(); it != mp2.end(); it ++) {
if (res.find(it->first) != res.end()) {
res[it->first] -= it->second;
} else {
res[it->first] = -it->second;
}
}
return res;
}
int main()
{
cin >> n >> m >> t;
for (int i = 1; i <= n; i ++) {
int a, b;
cin >> a >> b;
mp1[b] = a;
}
for (int i = 1; i <= m; i ++) {
int a, b;
cin >> a >> b;
mp2[b] = a;
}
map<int, int> res;
if (!t) {
res = Addition(mp1, mp2);
} else {
res = Subtraction(mp1, mp2);
}
int f = 0;
for (auto it = res.begin(); it != res.end(); it ++) {
if (it->second != 0) {
f = 1;
break;
}
}
if (!f) {
cout << "0";
return 0;
}
int flag = 0;
for (auto it = res.begin(); it != res.end(); it ++) {
if (it->second == 0) {
continue;
}
if (flag == 0 && it->second < 0) {
cout << "-";
} else if (flag != 0 && it->second > 0) {
cout << "+";
}
if (it->first == 0) {
cout << it->second;
} else if (it->first == 1) {
if (it->second == 1) {
cout << "x";
} else if (it->second == -1) {
cout << "-x";
} else {
cout << it->second << "x";
}
} else {
if (it->second == 1) {
cout << "x^" << it->first;
} else if (it->second == -1) {
cout << "-x^" << it->first;
} else {
cout << it->second << "x^" << it->first;
}
}
flag = 1;
}
return 0;
}
标签:map,res,mp1,XDOJ,730,second,mp2,Copilot,first From: https://blog.csdn.net/2301_79398241/article/details/144831329