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的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
随机推荐
- MyEclipse/Eclipse导入sun.misc.BASE64Encoder jar包步骤
1.右键项目 -->Properties -->Java Bulid Path-> Libraries -->JRE System Library-->Access ru ...
- 普通文件迁移ASM
1.ASM Access Through FTP and HTML Using XDB Configuration 2.rman 3.dbms_file_transfer 4.11g asmcmd 利 ...
- 怎么找到MyEclipse->add struts capabilities
问:MyEclipse:我的工程右键可以MyEclipse->add struts capabilities之前有,后来不知怎么就没有了,谁知道怎么弄出来是struts 答:你已经add过一次了 ...
- 【转】Tomcat配置文件入门
Tomcat 基本配置 tomcat读取配置文件 首先简单说一下tomcat是如何读取配置文件的.tomcat在启动时,首先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOM ...
- JDBC 与ODBC的区别
一.ODBC(Open DataBase Connectivity : 开放数据库连接) ODBC 总体结构 应用程序 执行处理并调用odbc函数,提交sql语 ...
- Simple XML
官网:http://simple.sourceforge.net/home.php 截止目前最新版本:simple-xml-2.7.1.jar 特点: jar lib文件只有360K左右的大小 它的使 ...
- 点亮一个led
1:RS232电平:计算机串口 高电平为-12v,低电平为+12v,所以计算机与单片机进行通信的时候需要加电平转换芯片max232(高电平为-9到-12,低电平为+3到+12之间的. max232通常 ...
- [LeetCode] Two Sum水过
刷LeetCode的第一题,TwoSum,基本算是水过. 题目:https://leetcode.com/problems/two-sum/ Given an array of integers, f ...
- [转载]su认证失败
Ubuntu 安装后,root用户默认是被锁定了的,不允许登录,也不允许 "su" 到 root.有人说这是个不好的实践,特别是对于服务器来说.我觉得对于桌面用户来说,这样安全性更 ...
- HDU-4651 Partition 整数拆分,递推
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:求n的整数拆为Σ i 的个数. 一般的递归做法,或者生成函数做法肯定会超时的... 然后要 ...