Multiplication and addition operation of unary polynomial "stl-map hash-map Reverse iterator traversal + 0 polynomial"

Source: Internet
Author: User

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

Input format:

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:

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.

Input Sample:
4 3 4 -5 2  6 1  -2 03 5 20  -7 4  3 1
Sample output:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 15 20 -4 4 -5 2 9 1 -2 0


Data Test Supplement:
4 3 4-5 2  6 1  -2 5-7  4 3  1
Output:
15 24-25 22 30 21-10 20-21 8 35 6-33 5 14 4-15 3 18 2-6 15 20-4 4-5 2 9 1-2 0

2 1 2 1 02 1 2-1 0
Output:
1 4-1 02 2

2-1000 1000 1000 02 1000 1000-1000 0
Output:
-1000000 2000 2000000 1000-1000000 00 0

01 999 1000
Output:
 0 0999 

The code should be over before the test data of the silk foot is over.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <map > #include <algorithm>using namespace std;int N, m;struct node{int x, z;}    A[200], B[200];int main () {int i, J;    scanf ("%d", &n);    for (i=0; i<n; i++) {scanf ("%d%d", &a[i].x, &a[i].z);    } scanf ("%d", &m);    for (i=0; i<m; i++) {scanf ("%d%d", &b[i].x, &b[i].z);    } map<int,int>q;    int cur;            For (i=0, i<n; i++) {for (j=0; j<m; J + +) {cur=a[i].z+b[j].z;        q[cur]+= (a[i].x*b[j].x);    }} int cnt=0;    BOOL Flag=false;    Map<int, Int>::reverse_iterator it=q.rbegin ();            for (it; It!=q.rend (); it++) {if (Flag==false) {if (it->second==0) continue;                else{flag=true; printf ("%d%d", It->second, It->first);            cnt++;            }}else{if (it->second==0) continue;else printf ("%d%d", It->second, It->first);        cnt++;    }} if (cnt==0) {printf ("0 0");    }printf ("\ n");    Map<int, int>h;    for (i=0; i<n; i++) {h[a[i].z]+=a[i].x;    } for (j=0; j<m; J + +) {h[b[j].z]+=b[j].x;    } Flag=false; It=h.rbegin ();    cnt=0;            for (it; It!=h.rend (); it++) {if (Flag==false) {if (it->second==0) continue;                else{flag=true; printf ("%d%d", It->second, It->first);            cnt++;            }}else{if (it->second==0) continue; else printf ("%d%d", It->second, It->first);        cnt++;    }} if (cnt==0) {printf ("0 0");//If there is no legal output, the output is 0 0;    }printf ("\ n"); return 0;}

Multiplication and addition operation of unary polynomial "stl-map hash-map Reverse iterator traversal + 0 polynomial"

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.