PAT-甲级-1009 Product of Polynomials

这篇博客介绍了PAT甲级考试中的一道题目——1009 Product of Polynomials,要求求解两个多项式的乘积。博主提供了两种解题思路,第一种未能通过所有测试点,第二种思路成功实现了多项式乘法,通过了所有测试。计算过程中,博主采用数组存储指数和系数,利用“+=”操作符合并相同指数的系数。最后,博主计划回头检查第一种思路的错误所在。
摘要由CSDN通过智能技术生成

1009 Product of Polynomials (25)(25 分)

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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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

题目就是多项式乘法

第一版(测试点0未通过)

#include<stdio.h>
#include<iostream>
#include <stdlib.h>
#include<iomanip>
using namespace std;
int main(){
    double a[11][2],b[11][2],c[101][2];
    int n1,n2,i,j;
    cin>>n1;
    for(i = 0;i<n1;i++){
        cin>>a[i][0]>>a[i][1];
    }
    cin>>n2;
    for(i = 0;i<n2;i++){
        cin>>b[i][0]>>b[i][1];
    }
    int k=0;

    for(i = 0;i<n1;i++){
        for(j = 0;j<n2;j++){
            c[k][0] = a[i][0] + b[j][0];
            c[k][1] = a[i][1] * b[j][1];
            k++;
        }
    }

    int out_num = k;
    if(c[0][1]==0) out_num--;
   // cout<<endl<<c[0][1]<<" "<<out_num<<endl;
    for(i = 1;i<k;i++){
        for(j = 0;j<i;j++){

            if(c[i][0]==c[j][0]&&c[i][0]!=-1){
                c[j][1]+=c[i][1];
                if(!c[j][1]) out_num--;
                c[i][0] = -1;//这个加过去了,不要了
                out_num--;
            }
        }
    }
    double t1,t2;
    for(i = 1;i<k;i++){
        for(j = i;j<k;j++){
            if(c[j][0]>c[i][0]){
                t1 = c[j][0];
                t2 = c[j][1];
                c[j][0] = c[i][0];
                c[j][1] = c[i][1];
                c[i][0] = t1;
                c[i][1] = t2;
                }
            }
        }
   cout<<out_num;
   for(i = 0;i<out_num;i++) {
        cout<<" "<<c[i][0]<<" ";
   //     cout << fixed <<setprecision(2)<<c[1][0];
       // cout<<c[i][1];
        printf("%.1lf",c[i][1]);
//   //         cout<<" "<<c[i][0]<<" "<<c[i][1];
    }
    return 0;
}

换了种思路后(全部通过)

#include<stdio.h>
#include<iostream>
#include <stdlib.h>
#include<iomanip>
using namespace std;
int main(){
    double a[1001]={0},b[1001]={0},c[2001]={0};
    int i,j;
    int n1,n2;
    int k;
    cin>>n1;
    for(i = 0;i<n1;i++){
        cin>>k;
        double aa;
        cin>>aa;
        a[k]+=aa;
    }
    cin>>n2;
    for(i = 0;i<n2;i++){
        cin>>k;
        double bb;
        cin>>bb;
        b[k]+=bb;
    }
    for(i = 0;i<1001;i++){
        for(j = 0;j<1001;j++){
            if(a[i]&&b[j]){
              //  cout<<i<<" "<<j<<endl;
                c[i+j]+= a[i]*b[j];
             //   cout<<i+j<<" "<<c[i+j]<<"="<<a[i]<<"*"<<b[j]<<endl;
            }
        }
    }
    k=0;
    int num = 0;
    for(i = 0;i<2001;i++){
        if(c[i]){
            k++;
            if(i>num) num = i;
        }
    }
    cout<<k;
    for(i=num;i>=0;i--){
        if(c[i]){
            cout<<" "<<i<<" ";
            printf("%.1lf",c[i]);
        }
    }
    return 0;
}

上面思路大概是以数组下标做指数的存储方式,先读入到两个数组里(注意指数的范围是到1000,可以开大一点),然后乘,乘的过程中用“+=”可以直接完成同指数的系数合并。最后为了输出,要先统计一下有多少是系数非0,并且标记一下最大下标。最后输出非零项。

回头有空再看第一个思路哪儿错了吧……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值