zoukankan      html  css  js  c++  java
  • PAT1009:Product of Polynomials

    1009. Product of Polynomials (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

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

    Input Specification:

    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 polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

    Output Specification:

    For each test case you should output the product 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 up 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

    思路

    简单题,求两个多项式的乘积。
    1.用一个vector poly存放系数乘积的结果,它的下标就是x的幂次方。两个多项式相乘后的最大次幂不会超过2000(因为Ni <= 1000)。

    2.用一个countx统计不同次幂的个数,如果是第一次相加不为0,那countx加1。如果系数相加为0.那么countx就得减1,

    3.输出countx,然后倒序输出poly中输出系数不为0的x幂次方就行。

    代码
    #include<iostream>
    #include<vector>
    #include<iomanip>
    using namespace std;
    vector<double> poly(2001,0);
    
    class x
    {
    public:
        int exp;
        double co;
    };
    
    int main()
    {
       vector<x> fst,scd;
       int k1;
       cin >> k1;
       for(int i = 0;i < k1;i++)
       {
           x tmp;
           cin >> tmp.exp >> tmp.co;
           fst.push_back(tmp);
       }
       int k2;
       cin >> k2;
       for(int i = 0;i < k2;i++)
       {
           x tmp;
           cin >> tmp.exp >> tmp.co;
           scd.push_back(tmp);
       }
    
       int countx = 0;
       for(int i = 0;i < fst.size();i++)
       {
           for(int j = 0;j < scd.size();j++)
           {
               int index = fst[i].exp + scd[j].exp;
               double res = fst[i].co * scd[j].co;
               if(poly[index] == 0)
                 countx++;
               poly[index] += res;
               if(poly[index] == 0)
                 countx--;
           }
       }
    
       cout << countx;
       for(int i = poly.size() - 1;i >= 0;i--)
       {
          if(poly[i] != 0)
            cout << " " << i << " " << fixed << setprecision(1) << poly[i];
       }
       cout << endl;
    }
    

      

  • 相关阅读:
    松翰单片机_SN8F570310——INTR
    松翰单片机_SN8F570310——EINT
    松翰单片机_SN8F570310——PWM
    松翰单片机_SN8F570310——SPI
    松翰单片机_SN8F570310——IIC
    松翰单片机_SN8F570310——UART
    全志_input
    全志_ADC驱动
    git随笔
    全志_外部中断
  • 原文地址:https://www.cnblogs.com/0kk470/p/7746441.html
Copyright © 2011-2022 走看看