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的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
随机推荐
- RPi 2B UART作为调试口或者普通串口
/************************************************************************************** * RPi 2B UAR ...
- 陈正冲老师对于c语言野指针的解释
那到底什么是野指针呢?怎么去理解这个“野”呢?我们先看别的两个关于“野”的词: 野孩子:没人要,没人管的孩子:行为动作不守规矩,调皮捣蛋的孩子.野狗:没有主人的狗,没有链子锁着的狗,喜欢四处咬人. 对 ...
- (转)solr排序OOM解决方法
转自 http://topcat.iteye.com/blog/1293650 问题 lucene使用排序时会将被排序字段全部加入内存再进行排序,当多次使用不同字段进行排序时会造成OOM问题 解决方案 ...
- Java开源项目(备查)
转自:http://www.blogjava.net/Carter0618/archive/2008/08/11/221222.html Spring Framework [Java开源 J2EE框 ...
- CentOS 6.4利用xampp安装bugfree3
1.下载xampp 安装 http://www.apachefriends.org/zh_cn/xampp.html 直接执行.run文件安装 默认会安装到/opt/lampp 2 .启动xampp ...
- [LeetCode]切割字符串,使各个子串都是回文
题目:Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【转】shell中IFS用法
http://blog.itpub.net/27181165/viewspace-775820/ 一 IFS的介绍 Shell 脚本中有个变量叫IFS(Internal Field Seprato ...
- java对象和类学习
定义对象的类: 一个对象的状态(属性或特征)是指那些具有他们当前值的数据域 一个对象的行为是由方法定义的,调用对象的方法就是完成对象的一个动作 使用一个通用类来定义同一类型的对象.类是一个模板,一个对 ...
- 云计算分布式大数据Hadoop实战高手之路第八讲Hadoop图文训练课程:Hadoop文件系统的操作实战
本讲通过实验的方式讲解Hadoop文件系统的操作. “云计算分布式大数据Hadoop实战高手之路”之完整发布目录 云计算分布式大数据实战技术Hadoop交流群:312494188,每天都会在群中发布云 ...
- Java 的集合框架
Java集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一 ...