PAT学习日志(6)--A1009 Product of Polynomials

A1009 Product of Polynomials

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​N​1 ​​  N​2​​  aN​2​​  ... N​K​​  aNK

where K is the number of nonzero terms in the polynomial, N​i​​  and aN​i(i=1,2,⋯,K) are the exponents and coefficients, respectively.
It is given that 1≤K≤10,0≤NK<⋯<N​2<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

题目为基础题型,只需严格按照题目要求输入输出即可

初次代码

#include<cstdio>
const int n=1011;
double a[n]={0},b[n]={0},x[n]={0};
int main(){
    int k1,k2,c,num=0,i=0,k=0;
    double d;
    
    scanf("%d",&k1);
    for(int j=0;j<k1;j++){
    	scanf("%lf %lf",&a[i],&a[i+1]);
    	i+=2;
	}
	
    scanf("%d",&k2);
    for(int j=0;j<k2;j++){
    	scanf("%lf %lf",&b[k],&b[k+1]);
    	for(int y=0;y<n;y++){
    		c=(int)(b[k]+a[y]);
    		x[c]+=b[k+1]*a[y+1];
		}
		k+=2;
	}
	
	for(int j=0;j<n;j++){
		if(x[j]!=0.0)
			num++;
	}
	
    printf("%d",num);
    for(int j=n;j>=0;j--){
    	if(x[j]!=0.0)
        	printf(" %d %.1f",j,x[j]);
    }
}

提交后发现,测试点3和4都提示错误

关于测试点4

审题后发现,两个最高次幂为1000的多项式相乘,最高幂次可以达到两千
所以定义数组x[]长度应该为2倍的a或b的长度,即2*n

更改后如下:

#include<cstdio>
const int n=1011;
double a[n]={0},b[n]={0},x[n+n]={0};
int main(){
    int k1,k2,c,num=0,i=0,k=0;
    double d;
    
    scanf("%d",&k1);
    for(int j=0;j<k1;j++){
    	scanf("%lf %lf",&a[i],&a[i+1]);
    	i+=2;
	}
	
    scanf("%d",&k2);
    for(int j=0;j<k2;j++){
    	scanf("%lf %lf",&b[k],&b[k+1]);
    	for(int y=0;y<n;y++){
    		c=(int)(b[k]+a[y]);
    		x[c]+=b[k+1]*a[y+1];
		}
		k+=2;
	}
	
	for(int j=0;j<2*n;j++){
		if(x[j]!=0.0)
			num++;
	}
	
    printf("%d",num);
    for(int j=2*n;j>=0;j--){
    	if(x[j]!=0.0)
        	printf(" %d %.1f",j,x[j]);
    }
}

测试点4正确,测试点3依旧错误

关于测试点3

对比《算法笔记》中的代码以及网上各种正确代码后,我发现我与他们唯一的不同在于我在给c赋值时使用的是c=(int)(b[k]+a[y])
即将double型通过(int)强转为int型再存储至c中
(int)强转时会直接舍去小数点后的值
若数据过大的极端情况可能会造成出现bug

所以第一个数不能用double型进行存储

修改代码如下:

#include<cstdio>
const int n=1011;
int  a[n]={0};
double b[n]={0},x[n+n]={0};
int main(){
    int k1,k2,c,num=0,i=0,k=0;
    double d;
    
    scanf("%d",&k1);
    for(int j=0;j<k1;j++){
    	scanf("%d",&a[j]);
    	scanf("%lf",&b[a[j]]);
	}
	
    scanf("%d",&k2);
    for(int j=0;j<k2;j++){
    	int h;
    	double g;
    	scanf("%d %lf",&h,&g);
    	for(int y=0;y<k2;y++)
    		 x[a[y]+h]+=g*b[a[y]];
	}
	
	for(int j=0;j<2*n;j++){
		if(x[j]!=0.0)
			num++;
	}
	
    printf("%d",num);
    for(int j=2*n-1;j>=0;j--){
    	if(x[j]!=0.0)
        	printf(" %d %.1f",j,x[j]);
    }
}

至此完全正确

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值