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的字符串.字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区 ...
随机推荐
- Mongodb操作总结
1.Mongovue里面可以直接group by ,这个时候一定要注意,group by的任何条件的是 json, 注意当值是Int,非string型的时候,值不要加上双引号 2.注意group by ...
- windows下python配置
1.下载安装 Pythonpython-2.7.5.msi http://www.python.org/download/如下载 Python 2.7.5,安装目录为 C:\Python27 2.添 ...
- 【翻译】运行于x86机器上的FreeBSD的PCI中断
来源 http://people.freebsd.org/~jhb/papers/bsdcan/2007/article/article.html 摘要 在拥有多个独立设备的计算机里一个重要的元素是一 ...
- 山东省2016acm省赛
A 水 #include <iostream> #include <cstdio> #include <algorithm> #include <list&g ...
- rtems总结
rtems 历史背景及现状 常用的api 和参数介绍 rtems_interrupt_enable rtems_interrupt_is_in_progress rtems_cache_flush_r ...
- xcode archive导出ipa时重签名
折腾了一晚上用另外的签名从xcode archive导出ipa,最后发现居然是没有导入p12文件的原因.... 顺便把参考的帖子记录一下: http://stackoverflow.com/quest ...
- Redhat Enterprise Linux 6.4图形界面的中文问题
一.界面中文,但Windows中的中文文件名上传到linux后乱码. .bashrc文件: export LANG=zh_CN.UTF-8 /etc/sysconfig/i18n文件: LANG=&q ...
- JDBC 创建连接对象的三种方式 、 properties文件的建立、编辑和信息获取
创建连接对象的三种方式 //第一种方式 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ ...
- 【转】logger
一个使用java.util.logging.Logger进行log输出的示例代码如下: package com.sample; import java.io.File; import java.uti ...
- [iOS基础控件 - 6.0] UITableView
A.需要掌握的 1.基本属性和方法 设置UITableView的dataSource.delegate UITableView多组数据和单组数据的展示 UITableViewCell的常见属性 UIT ...