Pat Grade-1009. Product of polynomials (25) unary polynomial multiplication

Source: Internet
Author: User
Tags time limit
1009. Product of polynomials (+)Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B
Program Standard author CHEN, Yue

This is supposed to find a*b where A and B are both polynomials.

Input Specification:

Each input file contains the one test case. Each case occupies 2 lines, and all line contains the information of a polynomial:k N1 aN1 N2 aN2 ... NK ANK, where K is the number of nonzero terms in the polynomial, Ni and ANi (I=1, 2, ..., K) are the exponents and Coeffi Cients, respectively. It is given that 1 <= K <=, 0 <= NK < ... < N2 < N1 <=1000.

Output Specification:

For each test case you should output the product of A and B on one line, with the same format as the input. Notice that there must is NO extra space at the end of each line. Accurate to 1 decimal place. Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6

The exponent and coefficients of k non-0 items of two polynomial are given, and the number of non-0 items and the exponent and coefficients of each of the outputs are multiplied.

The coefficient result retains one decimal.

#include <bits/stdc++.h> using namespace std; #define INF 0xfffffff #define MAXN 10010 int main () {#ifdef Online_judge #else freopen ("F:/cb/read.txt", "R", stdin);
/freopen ("F:/cb/out.txt", "w", stdout);
    #endif Ios::sync_with_stdio (FALSE);
    Cin.tie (0);
    int n;
    Double a[maxn],b[maxn],ans[maxn];//Phase Multiplier Group Open Large memset (a,0,sizeof (a));
    Memset (b,0,sizeof (b));
    memset (ans,0,sizeof (ans));
    cin>>n;
    int ma=0,mb=0,cnt=0;
        while (n--) {int x;
        Double y;
        cin>>x>>y;
        Ma=max (MA,X);
    A[x]=y;
    } cin>>n;
        while (n--) {int x;
        Double y;
        cin>>x>>y;
        Mb=max (MB,X);
    B[x]=y;
    } for (int i=0, i<=ma; ++i) for (int j=0; j<=mb; ++j) ans[i+j]+= (A[i]*b[j]);
    int POS=MA+MB;
    for (int i=ma+mb; i>=0;-I.) if (ans[i]!=0) ++cnt,pos=min (pos,i);
    cout<<cnt<< "";
   for (int i=ma+mb; i>pos;-I.)     if (ans[i]!=0) cout<<i<< "" <<setiosflags (ios::fixed) <<setprecision (1) <<ans[i]<
    < "";
 cout<<pos<< "" <<setiosflags (ios::fixed) <<setprecision (1) <<ans[pos]<<endl;}

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.