1002. A+B for Polynomials 解析

原创 2017年01月03日 15:53:33

这个题坑点在于

一、第一个是在于输出

题目要求的输出是精确到一位小数。所以在输出的时候要控制输出的格式。

而且最后一个结果的输出后面是不能带有空格的。最后一个输出需要单独判定。

二、在于两个多项式

两个多项式系数相加是会有0的情况出现的!!而题目要求输出是不能有0项的。

注意到这两个应该可以敲出代码了。


附上AC代码。

#include <iostream>
#include <vector>

using namespace std;

struct Node
{
	int k;
	float An;
};

void printvet(vector<Node> & poly);




int main() {
	

	vector<Node> poly1;
	vector<Node> poly2;
	vector<Node> poly3;

	Node temp;

	int n1, n2;
	cin >> n1;
	if (n1 > 500) while (1);
	for (int i = 0; i < n1; i++) {
		cin >> temp.k;
		cin >> temp.An;
		poly1.push_back(temp);
	}

	cin >> n2;
	for (int i = 0; i < n2; i++) {
		cin >> temp.k;
		cin >> temp.An;
		poly2.push_back(temp);
	}

	int j = 0, k = 0;
	while (j<poly1.size()&&k<poly2.size())
	{
		if (poly1[j].k == poly2[k].k) {
			poly1[j].An += poly2[k].An;
			if(poly1[j].An != 0) //和为0不添加
				poly3.push_back(poly1[j]);
			j++; k++;
		}
		else if(poly1[j].k < poly2[k].k)
		{
			poly3.push_back(poly2[k]);
			k++;
		}
		else
		{
			poly3.push_back(poly1[j]);
			j++;
		}
			
	}
	while (j <poly1.size())
	{
		poly3.push_back(poly1[j]);
		j++;
	}
	while (k <poly2.size())
	{
		poly3.push_back(poly2[k]);
		k++;
	}
	printvet(poly3);

	system("pause");

	return 0;
	
}

void printvet(vector<Node> & poly) {//打印函数
	
	if (poly.size() == 0) //多项式为0 只输出0
		cout << "0" << endl;
	else
		cout << poly.size() << " ";

	for (int i = 0; i < poly.size(); i++)
	{
		if (i == poly.size() - 1) { //最后一行不能有空格
			printf("%d %.1f", poly[i].k,poly[i].An);
		}
		else {
			printf("%d %.1f", poly[i].k, poly[i].An);
			cout << " ";
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

PAT—1002. A+B for Polynomials

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

1002. A+B for Polynomials (25)

1002. A+B for Polynomials (25) 思路及代码

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

1002. A+B for Polynomials (25)

#include #include struct data { double coe; int exp; }a[10], b[10], sum[20];int main() { ...

PAT 1002. A+B for Polynomials (25)

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

PAT (Advanced Level) Practise —— 1002. A+B for Polynomials

http://pat.zju.edu.cn/contests/pat-a-practise/1002 This time, you are supposed to find A+B wher...

[PAT-甲级]1002.A+B for Polynomials

1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Stan...

PAT A1002 A+B for Polynomials (25)

题目地址:https://www.patest.cn/contests/pat-a-practise/1002题目描述:This time, you are supposed to find A+B ...

PAT---A1002. A+B for Polynomials (25)

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

PAT 1002 A+B for Polynomials

PAT 1002 A+B for Polynomials

PAT1002-A+B for Polynomials

1002. A+B for Polynomials (25)This time, you are supposed to find A+B where A and B are two polynomi...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)