zoukankan      html  css  js  c++  java
  • 1002. A+B for Polynomials (25)

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

    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
    


    问题是要解决一个多项式的加法,这次吸取了之前的经验,先思考好才开始写,但是过程还是不是很顺利。
    1.没有认真读题,在输入中规定了It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.然后说输出格式跟输入一样,也就是N是依次递减的,理解错题意,花费了时间写了一个更新数组。
    2.Please be accurate to 1 decimal place. 这句话是精确到小数点一位,输出格式没写对
    3.最重要的一点,在看别人的代码的时候才发现,当一个项为0的时候就不需要输出了!

    #include <iostream>
    #include <cstdio>
    #include <iomanip>
    #include <map>
    
    
    using namespace std;
    
    map<int,double> m;
    
    int main()
    {
        int k1,k2;
        cin>>k1;
        int total=0;
        int sum[11];
        int j;
        for( j=0;j<k1;j++){
                int n;
                double an;
                cin>>n>>an;
                m[n]=an;
                sum[j]=n;
                total++;
        }
        cin>>k2;
        for(int i=0;i<k2;i++){
            int n;
            double an;
            cin>>n>>an;
            if(m.count(n))  m[n]=m[n]+an;
            else{
                m[n]=an;
                total++;
                sum[j+i]=n;
            }
        }
    
        int temp=0;
        for(int a=0;a<total;a++){
            for(int b=a;b<total;b++){
                if(sum[a]<sum[b]){
                  temp=sum[b];
                  sum[b]=sum[a];
                  sum[a]=temp;
                }
            }
        }
        int n=total;
        for(int i=0;i<total;i++){
                if(m[sum[i]]==0){
                    n--;
                }
        }
    
        cout<<n;
        for(int i=0;i<total;i++){
            if(m[sum[i]])
            printf(" %d %.1lf",sum[i],m[sum[i]]);
        }
    
        return 0;
    }





  • 相关阅读:
    左耳听风笔记之一
    富爸爸穷爸爸 -- 笔记
    Aruba无线控制器常用操作
    接入交换机办公网常用配置
    核心交换机办公网常用配置
    FortiGate防火墙办公网常用配置
    去掉深信服上网认证页面里的“修改密码”
    深信服上网行为管理短信认证多用户登录问题
    深信服上网行为管理配置跨三层MAC识别
    深信服上网行为管理实现一次认证成功之后连续3天无流量通过才再次认证
  • 原文地址:https://www.cnblogs.com/Qmelbourne/p/6052204.html
Copyright © 2011-2022 走看看