zoukankan      html  css  js  c++  java
  • PAT1002

    This time, you are supposed to find A+B where A and B are two polynomials.

    这一次,你被要求计算A+B当A和B是两个多项式的时候。

    Input

    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

    For each test case you should output the sum 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 to 1 decimal place.

    与输入格式相同的形式输出。注意,在每一行的结尾不要有多余的空格,请保留一位小数。

    Sample Input

    2 1 2.4 0 3.2
    2 2 1.5 1 0.5
    

    Sample Output

    3 2 1.5 1 2.9 0 3.2
    
     
    总结一下有几点,第一点题目意思需要读懂的同时注意题目的一些细节,比如需要保留小数。
    这道题目在想的时候,其实需要一点考虑的是数据结构。
    用什么样的数据结构去存会比较合理。
    下面是我的思考过程,一开始我就想到的时候就觉得数组是最快的,存放固定的常数位置对应的指数,然后对应的相加。
    看一下数据大小,1000,那么开1000大小的数组,然后最多数据是10+10个。也就是说,数组中会有很大一部分的空余。
    确实很浪费空间。然后我在想不浪费的话就是线性表了,也就是链表,链表插入是没有问题的,主要的问题是查询,查询到相应的位置,然后加数据,或者插入数据,这样的话因为最多是20个数据,就算不用二分查询,最坏复杂度也能在N!
    也就是(1+20)*20/2=210左右,比1000的数组循环一边要快。
     
    这是对于数据结构的想法,但是具体的题目来说,做题讲究一些代码的复杂程度,用数组实现比较方便,所以我依旧选择使用数组完成。
    下面是能过的代码。
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    
    using namespace std;
    
    float maps[1000];
    
    int main()
    {
        int n,x;
        float y;
        int aN=0;
        cin>>n;
        while (n--)
        {
            cin>>x>>y;
            maps[x] = y;
            aN++;
        }
        cin>>n;
        while (n--)
        {
            cin>>x>>y;
            if(maps[x] == 0)
                aN++;
            maps[x] += y;
            if(maps[x] == 0)
                aN--;
        }
        if(aN == 0)
        {
            cout<<aN;
            return 0;
        }
        else
        {
            cout<<aN<<" ";
        }
        for (int i = 1000; i >= 0; i--)
        {
            if(aN == 1 && maps[i] != 0)
            {
                printf("%d %.1f",i,maps[i]);
                break;
            }
            else if(maps[i] != 0)
            {
                printf("%d %.1f ",i,maps[i]);
                aN--;
            }
        }
        return 0;  
    }

    然后提一些需要改进的地方。

    输出的时候可以使用前缀空格输出。前面直接输出一个相数aN就行了,不用加空格。

    printf(" %d %.1f",i,maps[i]);

    这样可以减少判断最后输出格式的问题。

    其中还要注意的是,多项式加法之后会出现0

    所以会出现相数减少的情况,如果合并了,那么相数需要减少,这里我也错过,导致WA。

  • 相关阅读:
    mssql:tsql;创建表;给表添加约束;使用变量;事务,索引,视图;存储过程;触发器trigger;播放器http://www.smartgz.com/blog/Article/956.asp
    str.Replace(" ","");
    DataGrid分页;指定列的总和和平均值;显示鼠标背景色;弹出式窗口;
    .net 面试题 (1)
    数据绑定技术_单值数据绑定示例;将 DataTable,DataSet,DataView,DataReader 绑定到 DataGrid 控件示例;DataBinder.Eval;数组的值赋给ListBox1;Hashtable 绑定到;RadioButtonList;将XML 文件做为数据源绑定到控件
    Lession 17 Always young 保持年轻
    智力面试题
    Lession 16 A Polite request 彬彬有礼的要求
    几道 C 语言面试题
    建表的范例脚本,存储过程中参数的命名
  • 原文地址:https://www.cnblogs.com/linkstar/p/5668426.html
Copyright © 2011-2022 走看看