zoukankan      html  css  js  c++  java
  • 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:

    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[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不会超时,反而不会错,应该是测试数据没有很大的。

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    一点优化小知识
    网站结构优化之一
    [JOISC 2016 Day 3] 电报
    [HDU 6157] The Karting
    [JOISC 2015 Day2] Keys
    Educational Codeforces Round 107 (Rated for Div. 2)
    [JOISC 2020 Day4] 治疗计划
    CF1131G Most Dangerous Shark
    [APIO2016] 划艇
    [ICPC World Finals 2018] 绿宝石之岛
  • 原文地址:https://www.cnblogs.com/tccbj/p/10371224.html
Copyright © 2011-2022 走看看