设计函数分别求两个一元多项式的乘积与和。
输入分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
这是很坎坷的一道题,第一次数据结构实验就开始写了,然后写了两节课不行,各种错误(刚开始是用链表),然后中午回宿舍用map+vector写了一下,也AC了。然后接下来的一个多星期也在写这个题,用链表,因为当时数据结构还没讲链表,我链表的操作都是靠大一上的记忆+翻一下大一上的c语言书,总之很坎坷。然后最后终于写完正确之后,又用数组写了一遍,还是数组方便,链表确实有点麻烦。
数组那个刚才又重写了一遍,还算好看。至于剩下俩个,年段比较久远,不太好看。
主要就是写一个addData函数,把新的数据按照指数大小顺序(如果指数相同就系数相加)添加到数据集里。
#include <iostream>
#include <cstdlib>
using namespace std;
struct LNode
{
int data[1005][2];
int last;
};
typedef LNode *List;
List Read();
List JiaFa(List L1, List L2);
List ChengFa(List L1, List L2);
void AddDataToList(List L, int a, int);
void PrintList(List L);
int main()
{
List L1 = Read();
List L2 = Read();
List L3 = ChengFa(L1, L2);
List L4 = JiaFa(L1, L2);
PrintList(L3);
cout << endl;
PrintList(L4);
}
List Read()
{
int n;
int a, e;
List L = (List)malloc(sizeof(LNode));
L->last = -1;
cin >> n;
while (n--)
{
cin >> a >> e;
L->data[++L->last][0] = a;
L->data[L->last][1] = e;
}
return L;
}
List ChengFa(List L1, List L2)
{
List L3 = (List)malloc(sizeof(LNode));
L3->last = -1;
for (int i = 0; i <= L1->last; ++i)
{
for (int j = 0; j <= L2->last; ++j)
{
int a = L1->data[i][0] * L2->data[j][0];
int e = L1->data[i][1] + L2->data[j][1];
AddDataToList(L3, a, e);
}
}
return L3;
}
List JiaFa(List L1, List L2)
{
List L4 = (List)malloc(sizeof(LNode));
L4->last = -1;
for (int i = 0; i <= L1->last; ++i)
AddDataToList(L4, L1->data[i][0], L1->data[i][1]);
for (int i = 0; i <= L2->last; ++i)
AddDataToList(L4, L2->data[i][0], L2->data[i][1]);
return L4;
}
void AddDataToList(List L, int a, int e)
{
if (a == 0)
return;
int i;
for (i = 0; i <= L->last && L->data[i][1] > e; ++i)
;
//出循环 要么i现在时L-> last+ 1 那么前面的项 都>e
// 要么 现在这个数据结点的i<=e
if (i == L->last + 1)
{
L->data[++L->last][0] = a;
L->data[L->last][1] = e;
return;
}
//或者现在这个数据结点 相等
if (L->data[i][1] == e)
{
L->data[i][0] += a;
return;
}
//或者 这个还小:从这个开始到last都往右挪一个
for (int j = L->last; j >= i; --j)
{
L->data[j + 1][0] = L->data[j][0];
L->data[j + 1][1] = L->data[j][1];
}
L->data[i][0] = a;
L->data[i][1] = e;
L->last++;
return;
}
void PrintList(List L)
{
bool isAllZero = true;
for (int i = 0; i <= L->last; ++i)
{
if (L->data[i][0] != 0)
{
isAllZero = false;
break;
}
}
if (!isAllZero)
{
int cnt = -1;
for (int i = 0; i <= L->last; ++i)
{
if (L->data[i][0] == 0)
continue;
else
{
if ((++cnt) == 0)
cout << L->data[i][0] << " " << L->data[i][1];
else
cout << " " << L->data[i][0] << " " << L->data[i][1];
}
}
}
else
cout << "0 0";
}
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
bool cmp(pair<int, int> p1, pair<int, int> p2)
{
return p1.first > p2.first;
}
int main()
{
int N1, N2;
map<int, int> m1, m2, m3, m4;
cin >> N1;
int a, b;
while (N1--)
{
cin >> a >> b;
m1[b] = a;
}
cin >> N2;
while (N2--)
{
cin >> a >> b;
m2[b] = a;
}
for (auto it1 = m1.begin(); it1 != m1.end(); it1++)
{
for (auto it2 = m2.begin(); it2 != m2.end(); it2++)
{
m3[it1->first + it2->first] += it1->second * it2->second;
}
}
//错误思路: 如果指数相等,相加:其实不用。直接相加,如果系数相等就+=,不相等直接等于
//先算出相等的项*-1 ,再全加起来,
for (auto it = m1.begin(); it != m1.end(); it++)
{
m4[it->first] += it->second;
}
for (auto it = m2.begin(); it != m2.end(); it++)
{
m4[it->first] += it->second;
}
vector<pair<int, int>> v1;
for (auto it = m3.begin(); it != m3.end(); it++)
{
v1.push_back(make_pair(it->first, it->second));
}
vector<pair<int, int>> v2;
for (auto it = m4.begin(); it != m4.end(); it++)
{
v2.push_back(make_pair(it->first, it->second));
}
sort(v1.begin(), v1.end(), cmp);
sort(v2.begin(), v2.end(), cmp);
int flag = -1;
for (int i = 0; i < v1.size(); i++)
{
if (v1[i].second != 0)
{
flag = 1;
}
}
if (flag == 1)
{
for (int i = 0; i < v1.size(); i++)
{
if (v1[i].second == 0)
;
else
{
if (!i)
cout << v1[i].second << " " << v1[i].first;
else
cout << " " << v1[i].second << " " << v1[i].first;
}
}
}
else
{
cout << "0 0";
}
flag = -1;
puts("");
for (int i = 0; i < v2.size(); i++)
{
if (v2[i].second != 0)
{
flag = 1;
}
}
if (flag == 1)
{
for (int i = 0; i < v2.size(); i++)
{
if (v2[i].second == 0)
;
else
{
if (!i)
cout << v2[i].second << " " << v2[i].first;
else
cout << " " << v2[i].second << " " << v2[i].first;
}
}
}
else
cout << "0 0";
}
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct Node *List;
struct Node
{
int zhishu;
int xishu;
List next;
};
List addData(List L, int xishu, int zhishu);
void PrintList(List L)
{
if (L == nullptr)
{
cout << "0 0";
return;
}
int flag = 0;
List temp = L;
while (temp)
{
if (temp->xishu != 0) //有非零项
{
flag = 1;
break;
}
temp = temp->next;
}
if (flag == 0)
{
cout << "0 0";
}
else
{
int count = 0;
temp = L;
while (temp)
{
if (temp->xishu == 0)
{
temp = temp->next;
continue;
//如果直接continue的话,将陷入死循环!
}
if (count++)
{
cout << " ";
}
cout << temp->xishu << " " << temp->zhishu;
temp = temp->next;
}
}
}
int main()
{
int N;
cin >> N;
List p1 = nullptr, temp = nullptr;
//将第一行数据输入链表
while (N--)
{
List currNode = (List)malloc(sizeof(Node));
int a, b;
cin >> a >> b;
currNode->xishu = a;
currNode->zhishu = b;
currNode->next = NULL;
if (p1 == nullptr)
{
p1 = currNode;
temp = p1;
}
else
{
temp->next = currNode;
temp = temp->next;
}
}
cin >> N;
List p2 = nullptr;
temp = nullptr;
while (N--)
{
List currNode = (List)malloc(sizeof(Node));
int a, b;
cin >> a >> b;
currNode->xishu = a;
currNode->zhishu = b;
currNode->next = NULL;
if (p2 == nullptr)
{
p2 = currNode;
temp = p2;
}
else
{
temp->next = currNode;
temp = temp->next;
}
}
//乘法
List p3 = nullptr;
temp = nullptr;
List temp1 = p1, temp2 = p2; //temp1 2分别指向p1 p2
while (temp1)
{
temp2 = p2;
while (temp2)
{
int xishu = temp1->xishu * temp2->xishu;
int zhishu = temp1->zhishu + temp2->zhishu;
p3 = addData(p3, zhishu, xishu);
temp2 = temp2->next;
}
temp1 = temp1->next;
}
//加法
List p4 = nullptr;
temp = p1;
while (temp)
{
int xishu = temp->xishu;
int zhishu = temp->zhishu;
if (xishu == 0)
continue;
//即使这样,也不能保证里面没有非0项
p4 = addData(p4, zhishu, xishu);
temp = temp->next;
}
temp = p2;
while (temp)
{
int xishu = temp->xishu;
int zhishu = temp->zhishu;
if (xishu == 0)
continue;
p4 = addData(p4, zhishu, xishu);
temp = temp->next;
}
PrintList(p3);
cout << endl;
PrintList(p4);
}
List addData(List head, int zhishu, int xishu)
{
List currNode = (List)malloc(sizeof(Node));
currNode->zhishu = zhishu;
currNode->xishu = xishu;
currNode->next = NULL;
if (head == nullptr)
{
head = currNode;
return head;
}
//不是空链表
List pr = head, temp = head; //这个问题temp =null为什么会错!
while (pr->next != nullptr && pr->zhishu > zhishu)
{
temp = pr;
pr = pr->next;
}
//如果是在中间插入的
if (pr->zhishu <= zhishu)
{
//如果已经存在的
if (pr->zhishu == zhishu)
{
pr->xishu += xishu;
}
//需要添加一个结点的
else
{
if (pr == head)
{
currNode->next = head;
head = currNode;
}
else
{
pr = temp;
currNode->next = pr->next;
pr->next = currNode;
}
}
}
//是在最后插入的
else
{
pr->next = currNode;
}
return head;
}
安装babel插件npm install babel-loader @babel/core -D // babel与webpack通信的loadernpm install @babel/preset-env -D // ES6转ES5的工具npm install core-js regenerator-runtime -D // babel7.4之后的兼容浏览器规则包。babe...
注入的jar包如果不能直接使用 @autowired 来使用,可以采用如下方法:@Configurationpublic class DemoConfiguration {@Beanpublic Demo demo() {return new Demo(); //该对象为Jar包对象}}补充知识:引入第三方包 @Autowired Spring注入失败解决方案一、问题背景开发工程中,我负责的微服务...
“六宫粉黛无颜色,回眸一笑百媚生。”这首描写佳人的词语用在华阳夫人的身上并不为过。华阳夫人外貌出众,她是历史上不为史学家重视的众多女子中的一位,也是历史舞台上匆匆一现的昙花,但是正因为有了华阳夫人,整个历史的长河多了几分精彩,若不是她答应了吕不韦的请求,哪有后来一统六国的秦始皇和大秦帝国呢?《史记》记载:“孝文后曰华阳太后,与孝文王会葬寿陵。”史书上对华阳夫人的描写相当少,人们知晓她的事迹都是从其...
SSH 是一个强大且安全的工具,我们除了可以用它来远程管理主机外结合 sshfs 这个工具可以把远程主机的文件系统映射到本地主机上,透过 SSH 把远程文件系统挂载到本机上,这样我们可以不必使用 scp 工具就可以做到直接复制及删除远程主机的文件了,就像操作本地磁盘一样方便。sshfs 是基于 FUSE 构建的 SSH 文件系统客户端程序,通过它远程主机的配置无需作任何改变,就可以
前段时间在《编程之美》中也遇到了该问题,昨天在做中国某IT公司的在线笔试题的时候,也遇到了该问题,不过之前一直没有完全理解这个问题的真正解决方式,昨天晚上在睡觉前才想明白(不好意思,智商让各位捉急了 T_T)。N的阶乘(N!)中的末尾有多少个0? 如:N = 5,N! = 120.末尾有1个0.很多人遇到该问题的时候会想办法把阶乘的结果求出来 ,然后再去数结果的末尾有多少个0,但是
一、安装mysql 5.7.23rpm qa|grep tomcat 查看有多少mysql相关包 rpm -e -–nodeps 包名 --nodeps强制卸载查找之前老版本mysql的目录、并且删除老版本mysql的文件和库find / -name mysql查找结果如下:find / -name mysql/var/lib/mysql/var/lib/mysql/mysql/usr/lib64...
今天写了几个程序用来实现str函数,用来练习练习数组、指针和函数。1、strlen函数/*********************************************************************File Name: Author: xxx date:2016 11 29Descri
这几天弄vsftp,结果本地的帐号不能登录,,在登录的时候总是出现在我自己写的ftp程序中哈,出现我在发送命令pass的时候,格式是:pass(大写)+password /r/n的,命令和密码都是正确的,但是却总是要出现这个错误的response,我当时就物语了,错误为:500 OOPS: cannot change directory。自己就在网上查找资料,做出的更改是这样的:...
Samba 服务基础Samba是一个让不同系统之间通信的软件Samba是基于客户机/服务器型的协议SMB协议 (Server Message Block 服务消息块)CIFS协议(Common Internet File System 通用互联网文件系统)Samba软件包的构成服务端软件 samba客户端软件 samba-client用于提供服务端和客户端程序的公共组件 samba-commonSamba服务的程序组件smbd:为客户机提供服务器中共享资源的访问nmbd:提供基
–1、**LEFT()**方法—–函数说明—–1)语法:LEFT(character,integer)–2)介绍:参数1:要截取的字符串,参数2:截取字符个数,其实是从左边往右数3位–3)使用:–返回从字符串左边开始指定个数的字符–select LEFT(‘SqlServer_2008’,3)–4)返回:Sql–1、**RIGHT()**方法—– right()函数说明—–1)...
相信不少学习python的程序员都接触过string模块string模块主要包含关于字符串的处理函数多说无益,初学python的小伙伴还不赶紧码起来接下来将会讲到字符串的大小写、判断函数、以及字符串常规操作(填充、搜索、修改、剪切、添加、分割)1.大小写转换大小写转化在整个string操作中还是比较重要的,主要分三种类型第一种:全部大小写转化upper()与lower()两个函数如直译一样,将指定...
在使用wordcloud生成词云过程中,出现了OSError: cannot open resource错误,原因是字体属性font_path的设置与系统提供的字体不一致,系统提供的是ttf格式,所以应设置为ttf,而不能写为ttc.字体文件存放在 C:\Windows\Fonts目录下,可将对应的字体文件拷贝出来查看类型,将font_path设置为系统提供的字体即可。 ...