• 1002. A+B for Polynomials (25)


    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 <= 100 <= 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

    分析:题目是两个多项式相加。有坑点在第一次提交的时候没注意,只拿到了17分。由于采用了指数和系数的表示形式,那么当两个多项式相加的时候,就有可能某一项的系数为0,那么这一项就不用输出了。在做题目的时候想得还是不够全面。

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main()
    {
        double ans[1010];
        int flag[1010];
        memset(flag,0,sizeof(flag));
        int cnt=0;
        for(int input=0;input<2;input++)
        {
            int k;
            scanf("%d",&k);
            {
                for(int i=0;i<k;i++)
                {
                    int index;
                    double num;
                    cin>>index>>num;
                    if(flag[index]==0)
                    {
                        flag[index]=1;
                        cnt++;
                        ans[index]=num;
                    }
                    else
                    {
                        ans[index]+=num;
                        //判断系数是否为0,若为0去掉这一项
                        if(ans[index]<0.0000000001)
                        {
                            flag[index]=0;
                            cnt--;
                        }
                    }
                }
            }
        }
        cout<<cnt;
        int j=1010-1;
        for(;j>=0;j--)
        {
            if(flag[j]==1)
            {
                printf(" %d %.1lf",j,ans[j]);
            }
        }
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    VC编程锦集-1
    VC窗口关闭调用顺序
    Windows消息WM_USER、WM_APP的区别
    Toolbar添加控件;创建多行toolbar;重新排列toolbar
    SQL模糊查询详解
    MySql格式化日期
    Apache PHP 服务环境配置
    kvm.install
    repo搭建
    apache故障处理
  • 原文地址:https://www.cnblogs.com/xiongmao-cpp/p/6386220.html
Copyright © 2020-2023  润新知