题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009

分析:简单题。相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序。注意点:多项式相乘后指数最高可达2000。

题目描述:

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

参考代码:

#include<iostream>
#include<iomanip>
#include<string.h>
using namespace std; #define max 1000
double input1[max + 1];
double input2[max + 1];
double result[2*max + 1]; int main()
{
memset(input1,0,sizeof(input1));
memset(input2,0,sizeof(input2));
memset(result,0,sizeof(result));
int k;
int i,j;
int e;
//int temp=0; //记录最高指数
double c;
int count = 0; //最终输出的多项式的项数。
cin>>k;
for(i=0; i<k; i++)
{
cin>>e>>c;
input1[e] += c;
}
cin>>k;
for(i=0; i<k; i++)
{
cin>>e>>c;
input2[e] += c;
} for(i=0; i<=1000; i++)
{
for(j=0; j<=1000; j++)
{
result[i+j] += input1[i]*input2[j];
}
}
for(i=0; i<=2000; i++) if(result[i] != 0) count++;
cout<<count;
for(i=2000; i >= 0; i--)
{
if(result[i] != 0.0) {
cout<<" "<<i;
cout<<fixed<<setprecision(1);
cout<<" "<<result[i];
}
}
cout<<endl;
return 0;
}

【PAT】1009. Product of Polynomials (25)的更多相关文章

  1. PAT甲 1009. Product of Polynomials (25) 2016-09-09 23:02 96人阅读 评论(0) 收藏

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  2. PAT 甲级 1009 Product of Polynomials (25)(25 分)(坑比较多,a可能很大,a也有可能是负数,回头再看看)

    1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...

  3. pat 甲级 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. 【PAT甲级】1009 Product of Polynomials (25 分)

    题意: 给出两个多项式,计算两个多项式的积,并以指数从大到小输出多项式的指数个数,指数和系数. trick: 这道题数据未知,导致测试的时候发现不了问题所在. 用set统计非零项时,通过set.siz ...

  5. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  6. PATA 1009. Product of Polynomials (25)

    1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  7. 1009 Product of Polynomials (25分) 多项式乘法

    1009 Product of Polynomials (25分)   This time, you are supposed to find A×B where A and B are two po ...

  8. PAT甲级——1009 Product of Polynomials

    PATA1009 Product of Polynomials Output Specification: For each test case you should output the produ ...

  9. PAT 解题报告 1009. Product of Polynomials (25)

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

随机推荐

  1. VBScript使用CDO.Message发送邮件

    Const Email_From = "from@163.com" Const Password = "password" Const Email_To = & ...

  2. c#实现清理回收站垃圾

    /// <summary> /// 清理回收站垃圾 /// </summary> /// <param name="sender"></p ...

  3. C++学习笔记6:多文件编程

    1.添加文件到工程中: 2.函数调用时需要前向声明;以下为实例: //add.cpp int add(int x, int y) { return (x + y); } //main.cpp #inc ...

  4. select @@IDENTITY

    用select @@identity得到最新一次插入记录时自动产生的ID 如果你使用存储过程的话,将非常简单,代码如下:SET @NewID=@@IDENTITY 说明: 在一条 INSERT.SEL ...

  5. Hadoop学习记录(7)|Eclipse远程调试Hadoop

    1.创建Hadoop项目 2.创建包.类 这里使用hdfs.WordCount为例 3.编写自定Mapper和Reducer程序 MyMapper类 static class MyMapper ext ...

  6. 8个必备的PHP功能开发

    这篇文章主要介绍了8个必备的PHP功能开发,需要的朋友可以参考下 PHP开发的程序员应该清楚,PHP中有很多内置的功能,掌握了它们,可以帮助你在做PHP开发时更加得心应手,本文将分享8个开发必备的PH ...

  7. 前端小白第一次使用redux存取数据练习

    在学习了redux基本教程后,课程参考如下网址:https://www.redux.org.cn/docs/introduction/CoreConcepts.html,开始着手练习 1.首先编写一个 ...

  8. Phoenix 5.0 hbase 2.0 org.apache.hadoop.security.authentication.util.KerberosUtil.hasKerberosKeyTab

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  9. Java SSM框架之MyBatis3(六)MyBatis之参数传递

    一.单个参数  StudentParamsMapper package cn.cnki.ref.mapper; import cn.cnki.ref.pojo.Student; public inte ...

  10. http 状态码 码表

    HTTP状态码详解 - 查询资料 1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义 ...