PAT 1002 . A+B for Polynomials (25)多项式加法

原题目如下:


自己第一次写了下程序,开两个数组,然后在合并,再相加,效率很低,而且有几个测试点未通过

#include <iostream>
#include <iomanip>
#define max 1e5 
using namespace std;
int main()
{
	int temp11,temp21,num,mi;
	double xishu;
	cin>>temp11;
	int temp12[temp11];double temp13[temp11];
	for(int i=0;i<temp11;i++)
	cin>>temp12[i]>>temp13[i];	
	cin>>temp21;
	int temp22[temp21];double temp23[temp21];
	for(int i=0;i<temp21;i++)
	cin>>temp22[i]>>temp23[i];
	num=temp11+temp21;
	int temp1[num];//放入幂指数 
	double temp2[num];//放入系数 
	for(int i=0;i<temp11;i++)
	{
		temp1[i]=temp12[i];//放入第一个多项式的幂指数 
		temp2[i]=temp13[i];//放入第一个多项式的系数 
	}
	for(int j=0;j<temp21;j++)
	{
		temp1[temp11+j]=temp22[j];//放入第二个多项式的幂指数 
		temp2[temp11+j]=temp23[j];//放入第二个多项式的系数
	}
	for(int i=0;i<num;i++)
	{
		for(int j=i+1;j<num;j++)
		{
			if(temp1[i]==temp1[j])
			{
				num--;
				temp2[i]=temp2[i]+temp2[j];//相应系数相加 
				temp1[j]=max;//将与i相等的后面幂指数赋值无穷大 
			}
		}
	}
	for(int i=0;i<num;i++)
	{
		for(int j=i+1;j<num;j++)
		{
			if(temp1[i]<temp1[j])
			{
			
				mi=temp1[j];
				temp1[j]=temp1[i];
				temp1[i]=mi;	
				xishu=temp2[j];
				temp2[j]=temp2[i];
				temp2[i]=xishu;
			}
		}
	}
	cout<<num;
	for(int i=0;i<num;i++)
	{
		
		if(temp1[i]<max)
		{
			cout<<" ";
			cout<<setiosflags(ios::fixed)<<setprecision(1)<<temp1[i]<<' '<<temp2[i];
		}	
	}	
}


上面代码太过复杂,本来是一个简单问题,却写的这么长,主要原因是一开始拿到问题没有找到一个合适理想的数据结构存储,后来参考了下别人的方法,太赞了!如果将多项式存储在一个数组里面,数组下标元素为对应多项式幂指数,而数组中存储数值作为多项式系数,这样的话计算就会方便很多,新的代码如下:


#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	float poly[1001]={0};//这个数组用于存放幂指数为n时候,对应系数an是多少 
	int N1,N2,exp,number=0;//exp用于存放输入时候的幂指数
	float coe;//coe用于存放输入时候多项式的系数 
	cin>>N1;
	for (int i=0;i<N1;i++)
	{
		cin>>exp>>coe;
	//	if(poly[exp]==0)
	//	number++;
		poly[exp]=poly[exp]+coe;//有可能前后输入的两个多项式的幂指数是相同的
	}
	cin>>N2;
	for (int i=0;i<N2;i++)
	{
		cin>>exp>>coe;
	//	if(poly[exp]==0)
	//	number++;
		poly[exp]=poly[exp]+coe;
	}
	for (int i=1000;i>=0;i--)
	{
		if(poly[i]!=0)
		number++; 		
	}
	cout<<number;
	for (int i=1000;i>=0;i--)
	{
		if(poly[i]!=0)
		cout<<' '<<i<<' '<<fixed<<setprecision(1)<<poly[i];//注意这个地方保留一位小数用法 		
	}
	return 0;
}

源代码中没有用 if (poly[exp]!=0)  number++;这种方法来判别最终相加后的多项式之和的非零项个数,是因为有可能前后输入多项式相加刚好为0,这个时候number确增加了一个,这是错误的,所以最后不得已用了一个for循环来查找。找到这个原因之后,可以把代码再稍稍修改一下,下面的也是正确的

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	float poly[1001]={0};//这个数组用于存放幂指数为n时候,对应系数an是多少 
	int N1,N2,exp,number=0;//exp用于存放输入时候的幂指数
	float coe;//coe用于存放输入时候多项式的系数 
	cin>>N1;
	for (int i=0;i<N1;i++)
	{
		cin>>exp>>coe;
		if(poly[exp]==0)
		number++;
		poly[exp]=poly[exp]+coe;//有可能前后输入的两个多项式的幂指数是相同的
	}
	cin>>N2;
	for (int i=0;i<N2;i++)
	{
		cin>>exp>>coe;
		if(poly[exp]==0)
		number++;
		poly[exp]=poly[exp]+coe;
		if(poly[exp]==0)
		number--;//这里就是为了排除前后相加刚好多项式抵消这种结果
	}
	cout<<number;
	for (int i=1000;i>=0;i--)
	{
		if(poly[i]!=0)
		cout<<' '<<i<<' '<<fixed<<setprecision(1)<<poly[i];//注意这个地方保留一位小数用法 		
	}
	return 0;
}

综上来看,按照最终提交结果第二种方法是最简单的,占用内存最少。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值