The University of Zhejiang Pat 3-04. Multiplication and addition of unary polynomial (a solution to the problem)

Source: Internet
Author: User
Tags bool printf time limit
3-04. Multiplication and addition operations of unary polynomialTime limit MS
Memory Limit 32000 KB
Code length limit 8000 B
Standard of procedure for the award of questions

The product and sum of the two unary polynomials are obtained by the design function respectively.

input Format Description:

Input is divided into 2 lines, each line is given the number of non-0 polynomial, and then the exponential degradation way to enter a polynomial non-0 coefficients and exponents (absolute value is not more than 1000 integers). The numbers are separated by a space.

output Format Description:

The output is divided into 2 rows, and the coefficients and exponents of the product polynomial and the polynomial non-0 are output in exponential degradation mode respectively. The numbers are separated by a space, but cannot have extra spaces at the end.

sample input and output:

Serial number Input Output
1
4 3 4-5 2  6 1  -2 0 3 5  -7  4 3 1
24-25 21-10 20-21 8 6-33 5 4-15 3 2-6 1
5 20-4 4-5 2 9 1-2 0
2
2 1 2 1 0
2 1 2-1 0
1 4-1 0
2 2
3
2-1000 0
2 1000 1000-1000 0
-1000000 2000000 1000-1000000 0
0 0
4
0
1 999 1000
0 0
999 1000


The problem is to combine the previous two questions, Pat did not point to innovative consciousness ah, in addition to the code word or code word


#include <iostream> #include <deque> #include <algorithm> using namespace std;
	typedef struct {int XI;
int zhi;

}node;

Deque<node> A, B, C, D;
BOOL Haveflag = false;

bool Firstflag = true;
	void print (Node t) {if (T.xi = = 0) return;
	Haveflag = true;
		if (firstflag) {cout<<t.xi<< "" <<t.zhi;
	Firstflag = false;

} else cout<< "" <<t.xi<< "" <<t.zhi;}
	BOOL CMP (node T1, node T2) {if (T1.zhi > T2.zhi) {return true;
} return false;
	} int main () {int n;
	cin>>n;
		while (n--) {Node T;
		cin>>t.xi>>t.zhi;
	A.push_back (t);
	} cin>>n;
		while (n--) {Node T;
		cin>>t.xi>>t.zhi;
		B.push_back (t);
	D.push_back (t);
	} int i;
		for (i = 0; i < a.size (); ++i) {Node T;
		Int J;
			for (j = 0; J < b.size (); ++j) {t.xi = A[i].xi * B[J].XI;
			T.zhi = A[i].zhi + B[j].zhi; 
			int k;
			BOOL flag = TRUE; for (k = 0; k < c.size (); ++k) {if (c[k].zhi = = T.zhi) {c[k].xi + = T.xi;
				Flag = false;
		}} if (flag) c.push_back (t);
		} bool Flag1 = true;
				for (j = 0; J < d.size (); ++j) {if (D[j].zhi = = A[i].zhi) {d[j].xi + = A[i].xi;
			Flag1 = false;
	}} if (Flag1) D.push_back (A[i]);
	} sort (C.begin (), C.end (), CMP);
	For_each (C.begin (), C.end (), print);
	if (!haveflag) printf ("0 0");
	cout<<endl;
	Firstflag = true;
	Haveflag = false;
	Sort (D.begin (), D.end (), CMP);
	For_each (D.begin (), D.end (), print);
	if (!haveflag) printf ("0 0");
	cout<<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.