一元多項式的乘法與加法運算(20 分)
//初版18分,
/*
測試點 提示 結果 耗時 內存
0 sample換個數字 答案正確 3 ms 244KB
1 同類項合並時有抵消 答案正確 2 ms 240KB
2 系數和指數取上限,結果有零多項式 答案正確 2 ms 244KB
3 輸入有零多項式和常數多項式 段錯誤 2 ms 240KB
*/
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
typedef struct {
int zhishu;
int xishu;
}danxiang;
bool cmp(danxiang a,danxiang b)
{
return a.zhishu>b.zhishu;
}
int main()
{
vector<danxiang> a,b,jiafa,chengfa;
int n,zhishu,xishu;
cin>>n;
while(n--)
{
danxiang tmp;
cin>>tmp.xishu>>tmp.zhishu;
a.push_back(tmp);
}
cin>>n;
while(n--)
{
danxiang tmp2;
cin>>tmp2.xishu>>tmp2.zhishu;
b.push_back(tmp2);
}
//for(int i=0;i<a.size();i++)
// cout<<a[i].zhishu<<a[i].xishu;
//乘法
for(int i=0;i<a.size();i++)
{
for(int j=0;j<b.size();j++)
{
danxiang tmp3;
tmp3.xishu=a[i].xishu*b[j].xishu;
tmp3.zhishu=a[i].zhishu+b[j].zhishu;
chengfa.push_back(tmp3);
}
}
//排序+合並同類項
sort(chengfa.begin(),chengfa.end(),cmp);
int tmp;
if(chengfa.size()>1)
{
for(vector<danxiang>::iterator i=chengfa.begin()+1;i!=chengfa.end();i++)
{
if(i->zhishu==(i-1)->zhishu)
{
(i-1)->xishu+=i->xishu;
chengfa.erase(i);
}
}
}
int flag=0;
for(int i=0;i<chengfa.size();i++)
{
if(flag&&chengfa[i].xishu!=0) {
cout << " ";
}
if(chengfa[i].xishu!=0) {
cout << chengfa[i].xishu << " " << chengfa[i].zhishu;
flag = 1;
}
}
if(flag==0)
cout<<"0 0";
cout<<endl;
//加法
for(int i=0;i<a.size();i++)
jiafa.push_back(a[i]);
for(int i=0;i<b.size();i++)
jiafa.push_back(b[i]);
int cnt=0;
if(jiafa.size()>1)
{
for(vector<danxiang>::iterator i=jiafa.begin();i!=jiafa.end()-1;i++)
{
for(vector<danxiang>::iterator j=i+1;j!=jiafa.end();j++)
{
if(i->zhishu==j->zhishu)
{
i->xishu+=j->xishu;
cnt++;
jiafa.erase(j);
}
}
}
}
sort(jiafa.begin(),jiafa.end(),cmp);
flag=0;
for(int i=0;i<jiafa.size();i++)
{
if(flag&&jiafa[i].xishu!=0) {
cout<< " ";
}
if(jiafa[i].xishu!=0)
{
cout << jiafa[i].xishu << " " << jiafa[i].zhishu;
flag=1;
}
}
if(flag==0)
cout<<"0 0";
return 0;
}
終結版
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
typedef struct {
int zhishu;
int xishu;
}danxiang;
bool cmp(danxiang a,danxiang b)
{
return a.zhishu>b.zhishu;
}
int main()
{
vector<danxiang> a,b,jiafa,chengfa;
int n,zhishu,xishu;
cin>>n;
while(n--)
{
danxiang tmp;
cin>>tmp.xishu>>tmp.zhishu;
a.push_back(tmp);
}
cin>>n;
while(n--)
{
danxiang tmp2;
cin>>tmp2.xishu>>tmp2.zhishu;
b.push_back(tmp2);
}
//for(int i=0;i<a.size();i++)
// cout<<a[i].zhishu<<a[i].xishu;
//乘法
for(int i=0;i<a.size();i++)
{
for(int j=0;j<b.size();j++)
{
danxiang tmp3;
tmp3.xishu=a[i].xishu*b[j].xishu;
tmp3.zhishu=a[i].zhishu+b[j].zhishu;
chengfa.push_back(tmp3);
}
}
//排序+合並同類項
sort(chengfa.begin(),chengfa.end(),cmp);
int tmp;
if(chengfa.size()>1)
{
for(vector<danxiang>::iterator i=chengfa.begin()+1;i!=chengfa.end();i++)
{
if(i->zhishu==(i-1)->zhishu)
{
(i-1)->xishu+=i->xishu;
i->xishu=0;
}
}
}
int flag=0;
for(int i=0;i<chengfa.size();i++)
{
if(flag&&chengfa[i].xishu!=0) {
cout << " ";
}
if(chengfa[i].xishu!=0) {
cout << chengfa[i].xishu << " " << chengfa[i].zhishu;
flag = 1;
}
}
if(flag==0)
cout<<"0 0";
cout<<endl;
//加法
for(int i=0;i<a.size();i++)
jiafa.push_back(a[i]);
for(int i=0;i<b.size();i++)
jiafa.push_back(b[i]);
if(jiafa.size()>1)
{
for(vector<danxiang>::iterator i=jiafa.begin();i!=jiafa.end()-1;i++)
{
for(vector<danxiang>::iterator j=i+1;j!=jiafa.end();j++)
{
if(i->zhishu==j->zhishu)
{
i->xishu+=j->xishu;
j->xishu=0;
}
}
}
}
sort(jiafa.begin(),jiafa.end(),cmp);
flag=0;
for(int i=0;i<jiafa.size();i++)
{
if(flag&&jiafa[i].xishu!=0) {
cout<< " ";
}
if(jiafa[i].xishu!=0)
{
cout << jiafa[i].xishu << " " << jiafa[i].zhishu;
flag=1;
}
}
if(flag==0)
cout<<"0 0";
return 0;
}
段錯誤的原因總結
要理解段錯誤必須了解vector::erase()函數的使用方法,這個函數刪除一個元素,並且反而下一個元素的迭代器。但是注意,他的參數就是一個迭代器,所以他刪除元素之后,直接修改了迭代器,造成了vector訪問越界 ,產生segmentfault錯誤。
可以通過下面的代碼理解一下:
#include
#include
using namespace std;
int main()
{
vector<int> a;
for(int i=0;i<10;i++)
a.push_back(i);
/*
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
cout<<"地址: "<<&i<<" "<<"數值: "<<*i<<endl;
*/
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
{
cout<<"地址: "<<&i<<" "<<"數值: "<<*i<<endl;
a.erase(i);
cout<<"使用erase之后: "<<&i<<" "<<*i<<endl;
}
cout<<"刪除之后的"<<endl;
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
cout<<*i<<endl;
}
ps:可以看到迭代器i的地址在一個循環中始終不變,所以說i作為一個迭代器和指針類似,但是並不是指針,可能是他的數據區才用來指向迭代數據的地址
