标签:psu form color 计算 getc UNC test 结果 you
This time, you are supposed to find A×B where A and B are two polynomials.
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
K N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????
where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) are the exponents and coefficients, respectively. It is given that 1, 0.
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.
2 1 2.4 0 3.2
2 2 1.5 1 0.5
3 3 3.6 2 6.0 1 1.6
#include <stdio.h> #include <stdlib.h> 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不会超时,反而不会错,应该是测试数据没有很大的。
PAT A1009 Product of Polynomials (25 分)
标签:psu form color 计算 getc UNC test 结果 you
原文地址:https://www.cnblogs.com/tccbj/p/10371224.html