pat甲级 1002 A+B for 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 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​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

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

这道题的意思是多项式相加,即先有一个数N表示非零项的数目,后跟随多项式次数与多项式系数,求出两个多项式相加的结果。有两个输出的要求:1.是统计非零项 2.系数要保留一位小数。对我来说,一直卡住的是格式问题。要求最后不能有多余的空格,这个时候就应该考虑将空格放在数字前面进行输出,而非是将其放于数字后侧。一开始想复杂了,通过统计非零项的数目记作count,而后每输出一个系数和次数都用一个flag记录,flag还依次添加1.光从输出角度入手。只要判断当系数不为0时,便直接输出,为0时不输出即可,没有必要复杂化,利用flag出现的结果错误到最后还是没有解决。

整道题不难,由于次数是整数,不难想到利用数组去做,数组下标代表次数而值代表系数即可。由于我新学了map的迭代器和反向迭代器,所以就用一下稍微复杂些的方法。建立map < int,double> m,对于系数次数进行映射。最后通过反向迭代器反向输出(map内部通过红黑树进行键值顺序排序) 。

 

新单词:

polynomials  n.多项式

exponents    n.指数,幂

coefficients   n.系数; (测定物质某种特性的) 系数;

 

代码如下:

#include <bits/stdc++.h>

using namespace std;

map<int,double> m;

int main()
{
    int n1,n2;
    cin>>n1;
    int a;
    double b;
    while(n1--)
    {
        cin>>a>>b;
        m[a]=b;
    }
    cin>>n2;
    while(n2--)
    {
        cin>>a>>b;
        m[a]+=b;
    }

    int count=0;
    for(map<int,double>::reverse_iterator rit=m.rbegin();rit!=m.rend();rit++)
    {
        if(rit->second!=0)
        {
            count++;
        }
    }
    cout<<count;
    for(map<int, double>::reverse_iterator rit=m.rbegin(); rit!=m.rend(); rit++)
    {
        if(rit->second !=0.0)
        {
            cout<<" "<<rit->first<<" "<<fixed<<setprecision(1)<<rit->second;
        }
    }

    return 0;
}

 


以下是2020.1.21 用数组去做的

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

float shuzi[1010]={0};

int main()
{
    int k; //  k代表多项式中的非0项
    int n;//  n代表次数
    float a;//  a代表系数
    int nn=2;
    int count=0;
    while(nn--)
    {
        cin>>k;
        for(int i=0;i<k;i++)
        {
            scanf("%d",&n);
            scanf("%f",&a);   //lf代表输入的是double
            shuzi[n]+=a;
        }
    }
    for(int i=0;i<1010;i++)
    {
        if(shuzi[i]!=0.0)
        {
            count++;
        }
    }
    printf("%d",count);
    for(int j=1009;j>=0;j--)
    {
        if(shuzi[j]!=0.0)
        {
            printf(" %d %.1f",j,shuzi[j]);  //代表输出保留1位小数
        }
    }
    return 0;
}


//这题的数据结构可能要用上哈希?或者map ?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值