A+B for Polynomials(c++)

题目描述:

This time, you are supposed to find A+B where A and B are two polynomials.

输入描述:

Each input file contains one test case.
Each case occupies 2 lines, and each line contains the information of a polynomial:K N1 aN1 N2 aN2 … NK aNK,where K is the number of nonzero terms in the polynomialNi and aNi ​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively.
It is given that 1≤K≤10,0 <= NK < … < N2 < N1 <=1000

输出描述:

For each test case you should output the sum of A and B in one line, with the same format as the input.
Notice that there must be NO extra space at the end of each line.
Please be accurate to 1 decimal place.

测试样例:
输入:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

输出:

3 2 1.5 1 2.9 0 3.2

其实,这道题只要思路想通了就不难。在一开始的时候,我绕了不少弯路qwq。
这道题是意思是说,输入两行多项式,这两行中第一个数字K是该多项式的项数,然后后面2K个数字,分别是每一项的指数和系数。最后这两行对应相加(指数相同的项系数相加)。
最后,我看了一篇文章才知道怎么做。
先说思路:
只用一个数组,输入多项式时,它的指数作数组下标,它的系数作数组值。在输入第二多项式时,再在数组中指数作数组下标,系数作数组值。这样就能对应指数相同的项,系数相加。
然后就是代码:

#include<iostream>
using namespace std;
int main(){
	//没有什么事情是坚持做不到的
	double* data = new double[1001]();//这里的()作用是,把数组全部初始化为0
	int n;
	int zhishu;double xishu;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>zhishu>>xishu;
		data[zhishu] += xishu;
	}
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>zhishu>>xishu;
		data[zhishu] += xishu;
	}
	int sum = 0;//和的项数 
	for(int i=0;i<1001;i++){
		if(data[i]  > 1e-6){//double没有绝对的0,学了计算机原理就知道了
			sum++;
		}
	}
	cout<<sum;
	for(int i=1000;i>=0;i--){
		if(data[i] > 1e-6){
			printf(" %d %.1f",i,data[i]);
		}
	}
	return 0;
}

其实,只要找对了方向,每一道题给够了你时间,你就能做出来。有些时候要更相信自己一点点。不能因为一点点的难度就放弃。在坚持的时候也不要忘记提高自己的能力。

一个集坚强与自信于一身的菇凉。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡雅的惆怅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值