博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组
阅读量:4599 次
发布时间:2019-06-09

本文共 1788 字,大约阅读时间需要 5 分钟。

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:

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.22 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6
#include 
#include
struct poly{ int exp; float a;};poly p1[11];double p2[1000010] = { 0 };const double ep = 1e-4;int main(){ int k1, k2, exp; double a; scanf("%d", &k1); for (int i = 0; i < k1; i++){ scanf("%d %lf", &exp, &a); p1[i].exp = exp; p1[i].a = a; } getchar(); int count = 0; scanf("%d", &k2); for (int i = 0; i < k2; i++){ scanf("%d %lf", &exp, &a); for (int j = 0; j < k1; j++){ double tmp = p1[j].a*a; if (p2[exp + p1[j].exp] == 0 && tmp != 0){ count++; } p2[exp + p1[j].exp] += tmp; if (p2[exp + p1[j].exp] == 0)count--; } } printf("%d", count); for (int i = 1000001; i >= 0; i--){ if (p2[i] != 0.0){ printf(" %d %.1f", i, p2[i]); } } system("pause");}

注意点:一开始测试点0一直没通过,查了一下是相乘以后再加起来为0,然后以为是精度问题,发现怎么设置ep都不对,后来才知道原来是count计数错了,变为0的count会多加一次。当时在计算时就统计count是怕超时,结果发现多遍历一遍数count不会超时,反而不会错,应该是测试数据没有很大的。

转载于:https://www.cnblogs.com/tccbj/p/10371224.html

你可能感兴趣的文章
20145219 《Java程序设计》第06周学习总结
查看>>
C# 执行bat文件并取得回显
查看>>
基于YOLO的Autonomous driving application__by 何子辰
查看>>
javascript中的继承
查看>>
iOS-如何写好一个UITableView
查看>>
如何在Objective-C中实现链式语法
查看>>
select2 下拉搜索控件
查看>>
WebAPI常见的鉴权方法,及其适用范围
查看>>
08. 删除重复&海量数据
查看>>
重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++
查看>>
发布mvc遇到的HTTP错误 403.14-Forbidden解决办法
查看>>
记录一些好用的工具
查看>>
超链接样式设置(去下划线)(转)
查看>>
restcontroller和controller区别
查看>>
2016012003+陈琦+散列函数的应用及其安全性
查看>>
Android 状态栏通知Notification、NotificationManager详解
查看>>
Sublime Text 3中使用正则表达式删除空行
查看>>
UIApplicationDelegate协议
查看>>
再谈iOS 7的手势滑动返回功能
查看>>
Jmeter测试dubbo接口填坑
查看>>