PAT A1009 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 (,) are the exponents and coefficients, respectively. It is given that 1, 0.
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
#include <stdio.h>
#include <stdlib.h> struct poly{
int exp;
float a;
};
poly p1[];
double p2[] = { };
const double ep = 1e-;
int main(){
int k1, k2, exp;
double a;
scanf("%d", &k1);
for (int i = ; i < k1; i++){
scanf("%d %lf", &exp, &a);
p1[i].exp = exp;
p1[i].a = a;
}
getchar();
int count = ;
scanf("%d", &k2);
for (int i = ; i < k2; i++){
scanf("%d %lf", &exp, &a);
for (int j = ; j < k1; j++){
double tmp = p1[j].a*a;
if (p2[exp + p1[j].exp] == && tmp != ){
count++;
}
p2[exp + p1[j].exp] += tmp;
if (p2[exp + p1[j].exp] == )count--;
}
}
printf("%d", count);
for (int i = ; i >= ; i--){
if (p2[i] != 0.0){
printf(" %d %.1f", i, p2[i]);
}
}
system("pause");
}
注意点:一开始测试点0一直没通过,查了一下是相乘以后再加起来为0,然后以为是精度问题,发现怎么设置ep都不对,后来才知道原来是count计数错了,变为0的count会多加一次。当时在计算时就统计count是怕超时,结果发现多遍历一遍数count不会超时,反而不会错,应该是测试数据没有很大的。
PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组的更多相关文章
- 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 甲级 1016 Phone Bills (25 分) (结构体排序,模拟题,巧妙算时间,坑点太多,debug了好久)
1016 Phone Bills (25 分) A long-distance telephone company charges its customers by the following r ...
- PAT A1009 Product of Polynomials(25)
课本AC代码 #include <cstdio> struct Poly { int exp;//指数 double cof; } poly[1001];//第一个多项式 double a ...
- A1009 Product of Polynomials (25)(25 分)
A1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are tw ...
- 1009 Product of Polynomials (25分) 多项式乘法
1009 Product of Polynomials (25分) This time, you are supposed to find A×B where A and B are two po ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT A1075 PAT Judge (25 分)——结构体初始化,排序
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...
- PAT 甲级 1012 The Best Rank (25 分)(结构体排序)
题意: 为了评估我们第一年的CS专业学生的表现,我们只考虑他们的三个课程的成绩:C - C编程语言,M - 数学(微积分或线性代数)和E - 英语.同时,我们鼓励学生强调自己的最优秀队伍 - 也就是说 ...
- PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)
This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...
随机推荐
- [转]sqlserver转换为Mysql工具使用
https://files.cnblogs.com/files/miantiaoandrew/mss2sql_v5-3.zip 1.首先下载工具,链接如上 2.解压出来,运行mss2sql.exe 3 ...
- Linux常用基本命令( mkdir )
mkdir: 作用:创建目录( make directories ) 命令格式: make [option] 目录 1,创建目录, 当目录存在时,再次创建会提示文件已经存在 ghostwu@dev:~ ...
- python学习之老男孩python全栈第九期_day007作业
一.关系运算 有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合pythons={'alex','egon','yuanhao','wu ...
- 排序算法(7)--exchang Sorting--交换排序[2]--Quick Sort--快速排序
1.基本思想 从待排序列中选取一元素作为轴值(也叫主元). 将序列中的剩余元素以该轴值为基准,分为左右两部分.左部分元素不大于轴值,右部分元素不小于轴值.轴值最终位于两部分的分割处. 对左右两部分重复 ...
- 设计模式(13)--Chain of Responsibility(责任链模式)--行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一 ...
- 自定义jquery公用方法切换当前tab栏
经常在项目中需要写到切换当前栏目的展示效果,定义公共方法 //切换当前选中 /** * 全局切换或滑动标签的函数.一般使用active作为当前激活标签的类名,如果已使用其它类名,请将类名作为二参传入 ...
- h5向上翻页图标晃动动画,css固定h5向上翻页图标在页面上
//html结构<div class='upImg'><div> //css .upImg { background-image: url(../images/01.png); ...
- 在vue项目中,通过v-for循环,动态添加后台返回的事件
一.现有一种业务需求,前端的某个元素添加点击事件,但事件是后台返回的(不确定),需要动态添加,下面是具体思路: .假定后台返回数据为如下格式: list: [ { name: '李寻欢', kungF ...
- C# 如何防止重放攻击
重放攻击 重放攻击是指黑客通过抓包的方式,得到客户端的请求数据及请求连接,重复的向服务器发送请求的行为. 比如你有一个 “购买” 的操作,当你点击购买按钮时,向服务器发送购买的请求.而这时黑客对你的请 ...
- art-template模板应用
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...