甲级1002. A+B for Polynomials

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:

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 1K10,0NK​​<<N2​​<N1​​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




分析:将系数相同的项,变量相加,就ok了,但是注意一个地方,系数可以为0,但是变量不可以为0(哭了,不知道有这种规定),所以输出时把变量为0的剔除掉

代码如下:
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <unordered_map>
 5 using namespace std;
 6 
 7 struct term {
 8     int times;
 9     double num;
10 };
11 
12 bool cmp(term a, term b) { return a.times > b.times; }
13 
14 unordered_map<int, int> mapp;
15 vector<term> poly;
16 
17 int main()
18 {
19     int a, b, exp;
20     double coe;
21     scanf("%d", &a);
22     for (int i = 0; i < a; i++)
23     {
24         scanf("%d %lf", &exp, &coe);
25         auto it = mapp.find(exp);
26         if (it != mapp.end())
27         {
28             //在map中找到
29             poly[it->second].num += coe;
30         }
31         else
32         {
33             //没找到
34             int count = poly.size();
35             mapp[exp] = count;        //map中存入该键值对
36             term temp = { exp,coe };
37             poly.push_back(temp);
38         }
39     }
40     scanf("%d", &b);
41     for (int i = 0; i < b; i++)
42     {
43         scanf("%d %lf", &exp, &coe);
44         auto it = mapp.find(exp);
45         if (it != mapp.end())
46         {
47             //在map中找到
48             poly[it->second].num += coe;
49         }
50         else
51         {
52             //没找到
53             int count = poly.size();
54             mapp[exp] = count;        //map中存入该键值对
55             term temp = { exp,coe };
56             poly.push_back(temp);
57         }
58     }
59     sort(poly.begin(), poly.end(), cmp);
60     int count=0;
61     for (auto it = poly.begin(); it != poly.end(); it++)
62     {
63         if (it->num != 0)    count++;
64     }
65     cout << count;
66     for (auto it = poly.begin(); it != poly.end(); it++)
67     {
68         if(it->num!=0)
69             printf(" %d %.1lf", it->times, it->num);
70     }
71     return 0;
72 }

这里用了一个vector和一个 map,因为一开始看到这个想到的就是用数组,而寻找多项式的各个项对应的位置又想到了用字典。

 

看有人用的是map而不是unoder_map,然后利用map的自动键值从小到大排序来实现系数的有序。我使用数组来记录多项式,而他的做法用字典的键值对来记录多项式。

这里贴上他的做法,我觉得非常ok。


 1 #include<cstdio>
 2 #include<map>
 3 using namespace::std;
 4 map<int,double> s;
 5 int main()
 6 {
 7     int i1,t1;
 8     double t2;
 9     for(int z=0;z<2;z++)
10     {
11         scanf("%d",&i1);
12         for(int i=0;i<i1;i++)
13         {
14             scanf("%d%lf",&t1,&t2);
15             if(s.count(t1)==0)
16                 s[t1]=t2;
17             else
18                 s[t1]+=t2;
19         }
20     }
21     i1=0;
22     for(map<int,double>::const_iterator m_it=s.begin();m_it!=s.end();m_it++)
23     {
24         if(m_it->second!=0.0&&m_it->second!=-0.0)
25             i1++;
26     }
27     printf("%d",i1);
28     for(map<int,double>::reverse_iterator m_it=s.rbegin();m_it!=s.rend();m_it++)
29     {
30         if(m_it->second!=0.0&&m_it->second!=-0.0)
31             printf(" %d %.1lf",m_it->first,m_it->second);
32     }
33     printf("\n");
34     return 0;
35 }
36 
37 --------------------- 
38 作者:漂流瓶jz 
39 来源:CSDN 
40 原文:https://blog.csdn.net/qq278672818/article/details/54563738 
41 版权声明:本文为博主原创文章,转载请附上博文链接!


posted @ 2019-01-18 18:59  囧YY  阅读(112)  评论(0编辑  收藏  举报