选择题:
2-1采用多项式的非零项链式存储表示法,如果两个多项式的非零项分别为N1和N2个,最高项指数分别为M1和M2,则实现两个多项式相乘的时间复杂度是:(2分)
- O(N1×N2)
- O(M1×M2)
- O(N1+N2)
- O(M1+M2)
解析:要实现两个多项式的乘法,需要让一个多项式的每一项乘以另外一个多项式的每一项,因此这个题目的时间复杂度只跟非零项的个数有关,也就是跟N有关,与最高项的指数无关,也就是跟M无关。
编程题:
7-1 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
AC代码:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int num, eps;
node(int num, int eps = 0) : num(num), eps(eps) {}
bool operator<(const node &rhs) const
{
return eps > rhs.eps;
}
};
map<int, int> mp;
void print(vector<node> &mulity)
{
bool flag = false;
for(int i = 0; i < mulity.size(); i++)
{
if(mulity[i].num != 0)
{
if(!flag)
{
cout << mulity[i].num << " " << mulity[i].eps;
flag = true;
}
else
cout << " " << mulity[i].num << " " << mulity[i].eps;
}
}
if(!flag)
cout << "0 0";
cout << endl;
}
void input(vector<node> &G)
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
G.push_back(node(x, y));
}
}
int main()
{
vector<node> P, Q;
input(P);
input(Q);
vector<node> sum, mulity;
int s = 0, t = 0;
while(s < P.size() && t < Q.size())
{
if(P[s].eps == Q[t].eps)
{
if(P[s].num + Q[t].num != 0)
sum.push_back(node(P[s].num + Q[t].num, P[s].eps));
s++;
t++;
}
else if(P[s].eps > Q[t].eps)
{
sum.push_back(node(P[s].num, P[s].eps));
s++;
}
else
{
sum.push_back(node(Q[t].num, Q[t].eps));
t++;
}
}
while(s < P.size())
{
sum.push_back(node(P[s].num, P[s].eps));
s++;
}
while(t < Q.size())
{
sum.push_back(node(Q[t].num, Q[t].eps));
t++;
}
for(int i = 0; i < P.size(); i++)
{
for(int j = 0; j < Q.size(); j++)
{
if(!mp.count(P[i].eps + Q[j].eps))
{
mulity.push_back(node(P[i].num * Q[j].num, P[i].eps + Q[j].eps));
mp[P[i].eps + Q[j].eps] = mulity.size() - 1;
}
else
mulity[mp[P[i].eps + Q[j].eps]].num += P[i].num * Q[j].num;
}
}
sort(sum.begin(), sum.end());
sort(mulity.begin(), mulity.end());
print(mulity);
print(sum);
return 0;
}
注:这样的题本来代码就是比较乱,能用stl就用吧,但是不用stl行不行?行。
7-2 一元多项式求导 (20 分)
设计函数求一元多项式的导数。
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
AC代码1:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int num, eps;
node(int num = 0, int eps = 0) : num(num), eps(eps) {}
node(const node &rhs) : num(rhs.num), eps(rhs.eps) {}
bool operator<(const node &rhs) const
{
return eps > rhs.eps;
}
void fun()
{
num *= eps;
eps--;
}
};
map<int, int> mp;
void InNode(vector<node> &G)
{
int x, y;
while(cin >> x >> y)
G.push_back(node(x, y));
}
void print(const vector<node> &G)
{
bool flag = false;
for(int i = 0; i < G.size(); i++)
{
if(G[i].num == 0)
continue;
if(!flag)
{
cout << G[i].num << " " << G[i].eps;
flag = true;
}
else
cout << " " << G[i].num << " " << G[i].eps;
}
if(!flag)
cout << "0 0";
cout << endl;
}
void NodeFunc(vector<node> &G)
{
for(int i = 0; i < G.size(); i++)
G[i].fun();
}
int main()
{
vector<node> a;
InNode(a);
NodeFunc(a);
print(a);
return 0;
}
AC代码2:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int num;
int eps;
node(int num = 0, int eps = 0) : num(num), eps(eps) {}
node(const node &rhs) : num(rhs.num), eps(rhs.eps) {}
void fun()
{
this->num = this->num * this->eps;
this->eps--;
}
};
typedef node ElemType;
struct LNode
{
ElemType data;
LNode *next;
LNode() : data(0, 0), next(nullptr) {}
LNode(ElemType data, LNode *next = nullptr) : data(data), next(next) {}
};
typedef LNode *LinkList;
int main()
{
ios::sync_with_stdio(false);
LinkList L;
L = new LNode();
LNode *res = L;
int x, y;
while(cin >> x >> y)
{
LNode *p = new LNode(ElemType(x, y), nullptr);
res->next = p;
res = res->next;
}
LNode *prev;
prev = L;
res = L->next;
while(res)
{
res->data.fun();
if(res->data.num == 0)
{
LNode *temp = res;
prev->next = res->next;
res = res->next;
delete temp;
}
else
{
prev = res;
res = res->next;
}
}
res = L->next;
bool flag = true;
while(res)
{
if(flag)
{
cout << res->data.num << " " << res->data.eps;
flag = false;
}
else
cout << " " << res->data.num << " " << res->data.eps;
res = res->next;
}
if(flag)
cout << "0 0";
cout << endl;
return 0;
}
7-3 求链式线性表的倒数第K项 (20 分)
给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字。
输入格式:
输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理)。
输出格式:
输出倒数第K个位置上的数据。如果这个位置不存在,输出错误信息NULL
。
输入样例:
4 1 2 3 4 5 6 7 8 9 0 -1
输出样例:
7
AC代码(这个题数据跟题面有点矛盾,看看代码就好了):
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 10000000 + 5;
typedef int ElemType;
struct SQ
{
ElemType *elem;
int length;
SQ(ElemType *elem = NULL, int length = 0) : elem(elem), length(length) {}
}SqList;
int pos;
void InitSqList()
{
SqList.elem = new int[MAX_SIZE];
SqList.length = 0;
}
void InSqList()
{
int x, st = SqList.length;
while(cin >> x && x >= 0)
{
SqList.elem[st++] = x;
SqList.length++;
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> pos;
InitSqList();
InSqList();
pos = SqList.length - pos;
if(pos < 0 || pos > SqList.length - 1)
cout << "NULL" << endl;
else
cout << SqList.elem[pos] << endl;
return 0;
}