• PAT 1009. Product of Polynomials


    1009. 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: 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 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
    

    分析

    我是用map来做这题的,感觉没什么可说的key为次数,value为系数,map按key的非递减的顺序自动排序,输出时利用rbegin,rend,来输出就行了←_←

    代码如下

    #include<iostream>
    #include<map>
    using namespace std;
    int main(){
    	map<int,double> p1,p2,p;
    	int N,exp,cnt=0;
    	double coe;
    	cin>>N;
    	for(int i=0;i<N;i++){
    		cin>>exp>>coe;
    		p1[exp]=coe;
    	}
    	cin>>N;
    	for(int i=0;i<N;i++){
    		cin>>exp>>coe;
    		p2[exp]=coe;
    	}
    	for(auto a=p1.begin();a!=p1.end();a++){
    		for(auto b=p2.begin();b!=p2.end();b++){
    			p[a->first+b->first]+=a->second*b->second;
    		}
    	}
    	for(auto b=p.rbegin();b!=p.rend();b++)
    	if(b->second!=0) cnt++;
    	cout<<cnt;
    	for(auto b=p.rbegin();b!=p.rend();b++)
    	if(b->second!=0) printf(" %d %.1f",b->first,b->second);
    }
    
  • 相关阅读:
    python中如何将两个list合并成一个list,不用for语句
    python print的用法
    Pandas Timestamp 和 python 中 datetime 的互相转换
    python eval, exec. compile
    python 用 __all__ 暴露接口
    python functiontools 模块
    Python 修饰符, 装饰符
    Python 字典(Dictionary) update()方法
    Python :random 随机数生成
    Pandas dataframe 标记删除重复记录
  • 原文地址:https://www.cnblogs.com/A-Little-Nut/p/8192072.html
Copyright © 2020-2023  润新知