(甲)1009 Product of Polynomials (25 分)

题目:

This time, you are supposed to find A×BA\times BA×B where AAA and BBB 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:

KKK N1N_1N​1​​aN1a_{N_1}a​N​1​​​​N2N_2N​2​​aN2a_{N_2}a​N​2​​​​ ... NKN_KN​K​​aNKa_{N_K}a​N​K​​​​

where KKK is the number of nonzero terms in the polynomial, NiN_iN​i​​ and aNia_{N_i}a​N​i​​​​ (i=1,2,⋯,Ki=1, 2, \cdots , Ki=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤101\le K \le 101≤K≤10, 0≤NK<⋯<N2<N1≤10000 \le N_K < \cdots < N_2 < N_1 \le 10000≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the product of AAA and BBB 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

题意:

给你两个多项式A,B,让你求出来 A*B的结果;

给出来的数据有两行,每一行都是:一个数字K,代表后面有K对数据,后面跟着K对x,y,其中x代表着指数,y代表系数;

输出:一个数字Q,代表系数不为0的数据的个数,后面跟着Q对数据,分别是指数和系数,其中指数是整数,系数保留一位小数;

思路:

多项式乘积,直接循环就可以了,注意指数相加,系数相乘

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int k1,k2;
int a[2001];//指数;
double b[2001];//系数;
int x;
double y;
int sum;

int main()
{
    scanf("%d",&k1);
    for(int i=0; i<k1; i++)
    {
        scanf("%d%lf",&a[i],&b[i]);
    }
    scanf("%d",&k2);
    double w[2001];
    memset(w,0,sizeof w);//清零;
    for(int i=0; i<k2; i++)
    {
        scanf("%d%lf",&x,&y);
        for(int j=0; j<k1; j++)
        {
            w[x+a[j]]+=b[j]*y;//注意;
        }
    }
    sum=0;
    for(int i=2000; i>=0; i--)//判断系数不为0的个数;
    {
        if(w[i]!=0)
            sum++;
    }
    printf("%d",sum);
    for(int i=2000; i>=0; i--)
    {
        if(w[i]!=0)
            printf(" %d %.1lf",i,w[i]);//注意输出格式;
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/89739331