1002. A+B for Polynomials (25)
2016-03-29 18:31
295 查看
1002. A+B for Polynomials (25)
QuestionThis 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
这道题目的思路完全是按着人的直观想法来的。
用数组在表示一个多项式,每一个index表示次数,其中的value就是系数。所以要一个1001大小的、初始化为0的数组。
然后把第一个不等式的系数按照次数索引加进去,再把第二个不等式的系数按照次数索引加进去。
最后数一下数组中不为0的个数,就是新多项式的K,然后把数组倒着输出不为0的值即可啦!
最直观的代码
#include <iostream> using namespace std; const int MAX_EXP = 1001; int main(int argc, const char * argv[]) { double p1[MAX_EXP] = {0}, p2[MAX_EXP] = {0}, p3[MAX_EXP] = {0}; int K1, K2, exp; double coefficient; cin >> K1; for (int i = 0; i < K1; ++i) { cin >> exp >> coefficient; p1[exp] = coefficient; } cin >> K2; for (int i = 0; i < K2; ++i) { cin >> exp >> coefficient; p2[exp] = coefficient; } int K3 = 0; for (int i = 0; i < MAX_EXP; ++i) { p3[i] = p1[i] + p2[i]; if (p3[i] != 0) { ++K3; } } cout << K3; for (int i = MAX_EXP - 1; i >= 0; --i) { if (p3[i] != 0) { //这里一定要注意!输出的格式!保留1位小数! printf(" %d %.1lf", i, p3[i]); } } return 0; }
稍微改进一点儿
#include <iostream> using namespace std; const int MAX_EXP = 1001; int main(int argc, const char * argv[]) { double p[MAX_EXP] = {0}; int exp = 0, K = 0 ; cin >> K; for (int i = 0; i < K; i++) { cin >> exp; cin >> p[exp]; } cin >> K; for (int i = 0; i < K ;i++) { double temp = 0; cin >> exp >> temp; p[exp] += temp; } K = 0; for (int i = 0; i < MAX_EXP; i++) { if (p[i] != 0) { K++; } } cout << K; for (int i = MAX_EXP - 1; i >= 0; i--) { if (p[i] != 0) { //这里一定要注意!输出的格式!保留1位小数! printf(" %d %.1lf", i, p[i]); } } return 0; }
别人的解法
#include<iostream> #include <vector> #include <algorithm> #include <iomanip> using namespace std; bool compare(pair<int,float> a,pair<int,float> b) { return a.first > b.first; } int main() { int M,N; vector<pair<int,float> > vec; vector<pair<int,float> > vec1; vector<pair<int,float> >::iterator it,it1; float exp=0; float coe=0; cin>>M; while(M--) { cin>>exp>>coe; vec.push_back(make_pair(exp,coe)); } cin>>N; while(N--) { cin>>exp>>coe; vec.push_back(make_pair(exp,coe)); } sort(vec.begin(),vec.end(),compare); for (it=vec.begin();it!=vec.end();it=it1){ for (it1=it+1; it1!=vec.end() && (*it).first == (*it1).first; it1++) { (*it).second += (*it1).second; } if ((*it).second !=0) { vec1.push_back(*it); } } cout<<vec1.size(); for (it=vec1.begin();it!=vec1.end();it++) { cout<<" "<<(*it).first <<" "<<fixed<<setprecision(1)<<(*it).second; } cout<<endl; return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析