不难,只要注意0项不输出,从大到小输出即可。
1 #include<iostream> 2 #include<map> 3 #include<utility> 4 #include<iterator> 5 #include<iomanip> 6 using namespace std; 7 8 int main() 9 { 10 int ain, bin; 11 while(cin>>ain) 12 { 13 map<int, double> a, b, c; 14 for(int i=0; i < ain; ++i) 15 { 16 int key; double value; 17 cin>>key>>value; 18 a[key] = value; 19 } 20 cin>>bin; 21 for(int i=0; i < bin; ++i) 22 { 23 int key; double value; 24 cin>>key>>value; 25 b[key] = value; 26 } 27 map<int,double>::iterator a_iter = a.begin(); 28 for(; a_iter != a.end(); ++a_iter) 29 { 30 map<int,double>::iterator b_iter = b.begin(); 31 for(; b_iter != b.end(); ++b_iter) 32 { 33 int k = a_iter->first + b_iter->first; 34 c[k] += a_iter->second*b_iter->second; 35 } 36 } 37 map<int, double>::reverse_iterator c_riter = c.rbegin(); 38 int num=0; 39 for(; c_riter != c.rend(); ++c_riter) 40 if(c_riter->second != 0) 41 ++num; 42 cout<<num; 43 for(c_riter = c.rbegin(); c_riter != c.rend(); ++c_riter) 44 if(c_riter->second != 0) 45 cout<<fixed<<setprecision(1)<<" "<<c_riter->first<<" "<<c_riter->second; 46 cout<<endl; 47 } 48 return 0; 49 }