PAT 1009
1009. Product of Polynomials (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 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 #include <stdio.h>
2 #include <algorithm>
3 using namespace std;
4
5
6 typedef struct Poly{
7 int exp;
8 double coef;
9 }Poly;
int comp(const Poly&,const Poly&);
int main()
{
int KA,KB;
Poly A[],B[],C[];
int i,j;
while(scanf("%d",&KA) != EOF){
for(i=;i<KA;++i){
scanf("%d%lf",&A[i].exp,&A[i].coef);
}
scanf("%d",&KB);
for(i=;i<KB;++i){
scanf("%d%lf",&B[i].exp,&B[i].coef);
}
for(i=;i<KA;++i)
for(j=;j<KB;++j){
C[i * KB + j].exp = A[i].exp + B[j].exp;
C[i * KB + j].coef = A[i].coef * B[j].coef;
}
sort(C,C+KA*KB,comp);
double coef_sum = C[].coef;
int exp = C[].exp;
j = ;
for(i=;i<KA*KB;++i){
if(exp == C[i].exp){
coef_sum += C[i].coef;
}
else{
if (abs(coef_sum) > 0.000001){
C[j].exp = exp;
C[j++].coef = coef_sum;
}
exp = C[i].exp;
coef_sum = C[i].coef;
}
}
if (abs(coef_sum) > 0.000001){
C[j].exp = exp;
C[j++].coef = coef_sum;
}
printf("%d",j);
for(i=;i<j;++i){
printf(" %d %.1lf",C[i].exp,C[i].coef);
}
printf("\n");
}
return ;
}
int comp(const Poly &x,const Poly &y)
{
return y.exp < x.exp;
}
PAT 1009的更多相关文章
- PAT 1009 Product of Polynomials
1009 Product of Polynomials (25 分) This time, you are supposed to find A×B where A and B are two p ...
- PAT 1009 说反话 (20)(代码)
1009 说反话 (20)(20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空 ...
- PAT——1009. 说反话
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
- PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值
题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...
- PAT 1009. 说反话 (20) JAVA
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
- PAT 1009. 说反话 (20)
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
- PAT 1009 说反话
https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960 给定一句英语,要求你编写程序,将句中所有单词 ...
- PAT 1009. Triple Inversions (35) 数状数组
Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...
- PAT 1009 说反话 C语言
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
随机推荐
- 【转】FTS抓包看蓝牙的SDP整个过程
原文网址:http://blog.sina.com.cn/s/blog_69b5d2a50101f23c.html 1.概述 SDP是蓝牙的Service Discovery Protocol,用 ...
- Ruby基础类型,动态特性,代码块
#Ruby内置基础数据类型 NilClass,TureClass,FalseClass,Time,Date,String,Range,Struct,Array,Hash #Numerice 1.分为I ...
- 第一个MVC模式的程序
数据库 是一个SQL sever数据库,结构很简单,创建名为firstMVC的数据库,只包含一个数据表(名称为Persons),其中共有三列,分别用于保存人员(persons)的ID.姓名以及创建日期 ...
- 负载均衡、LVS概述
1. 负载均衡概述 负载均衡的基本思路是:在一个服务器集群中尽可能的平衡负载量.通常的做法是在服务器前端设置一个负载均衡器(一般是专门的硬件设备).然后负载均衡器将请求的连接路由到最空闲的可用服务器. ...
- C# DataGridView绑定数据源的几种常见方式
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...
- (转)Make命令简介与使用
转载自阮一峰的博客: http://www.ruanyifeng.com/blog/2015/02/make.html 代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编 ...
- HTTP 报文总结、外送两本电子书
写在前面的话:喜欢这个比喻:如果说HTTP是因特网的信使,那么HTTP报文就是它用来搬东西的包裹. HTTP是一个应用层协议,研究它的内容的确很枯燥,没啥意思,都是规定好的,我们只需要知道是什么就好了 ...
- 1.2CPU和GPU的设计区别
CPU和GPU之所以大不相同,是由于其设计目标的不同,它们分别针对了两种不同的应用场景.CPU需要很强的通用性来处理各种不同的数据类型,同时又要逻辑判断又会引入大量的分支跳转和中断的处理.这些都使得C ...
- C++&OpenCV中读取灰度图像到数组的两种
如标题所言,此处是对于灰度图像而言 ///method 1 read the image data one by one for (int row = 0, i = 0;row < imgDst ...
- [C++]VisualAssistX中文注释提示错误 解决办法
问题情况:Visual Assist X中文注释为提醒注释错误,而且在注释下面以红线标.问题原因:这是因为Visual Assist X认为中文的注释是拼写错误.问题处理: 1.打开 Microsof ...