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的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
随机推荐
- I.MX6 Android frameworks services 文件架构
/******************************************************************************* * I.MX6 Android fra ...
- SharePoint 2010 使用自定义aspx页面替换列表默认的新建(NewForm.aspx),查看(DispForm.aspx)和编辑(EditForm.aspx)页面
转:http://www.cnblogs.com/sygwin/archive/2011/11/04/2236678.html 如何使用自定义的aspx页(比如Application Page)替换列 ...
- [转] Envelop
xiaohaidepoli原文Envelop Envelope也称包络线,是一个矩形区域,是每个几何形体的最小外接矩形.每个Geometry都拥有一个Envelope,包括Envelope自身. 它定 ...
- HDU 5965 Gym Class 贪心+toposort
分析:就是给一些拓补关系,然后求最大分数,所以贪心,大的越靠前越好,小的越靠后越好 剩下的就是toposort,当然由于贪心,所以使用优先队列 #include <iostream> #i ...
- 设计模式_Proxy_代理模式
形象例子: 跟MM在网上聊天,一开头总是“hi,你好”,“你从哪儿来呀?”“你多大了?”“身高多少呀?”这些话,真烦人,写个程序做为我的Proxy吧,凡是接收到这些话都设置好了自动的回答,接收到其他的 ...
- mssql server 2005还原数据库bak文件与“备份集中的数据库备份与现有的xx数据库不同”解决方法
mssql server 2005还原数据库bak文件,网站使用虚拟主机建站会经常遇到,一般情况下,主机商有在线的管理程序,但有时候没有的话,就需要本地还原备份sql数据库了.这种情况mssql se ...
- HDU2015校赛 The Country List
今天手感真差..各种读错题意.水题... 就是说,给你几个串.如果长度一样并且相同位置字符相同(不分大小写)的个数大于两个就是不同串. #include<iostream> #includ ...
- 【转载】高性能I/O设计模式Reactor和Proactor
转载自:http://blog.csdn.net/roger_77/article/details/1555170 昨天购买了<程序员>杂志 2007.4期,第一时间去翻阅了一遍,其中有一 ...
- jqgrid使用sql row_number进行分页
背景 系统中使用了jqgrid的展示,现在要处理10w+的数据量 现状 使用了全查询的,查询到了10w+的数据放到了datatable中,每次页面刷新需要9秒多,并且传递给另一个dll来处理一些事情. ...
- hdoj 1799 循环多少次?
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...