PAT甲级1009

3/8/2017来源:ASP.NET技巧人气:2112

#include <iostream> #include <iomanip> #include <vector> using namespace std; /* run this PRogram using the console pauser or add your own getch, system("pause") or input loop */ struct Polynomials{ int exponent; float coefficient; }; int main(int argc, char** argv) { int K1,K2; float Poly3[2001]; Polynomials Poly1[10],Poly2[10]; for(int i=0;i<2001;i++){ Poly3[i]=0; } cin>>K1; for(int i=0;i<K1;i++){ cin>>Poly1[i].exponent>>Poly1[i].coefficient; } cin>>K2; for(int i=0;i<K2;i++){ cin>>Poly2[i].exponent>>Poly2[i].coefficient; } float Poly3Coefficient=0; int Poly3Exponent=0; for(int i=0;i<K1;i++){ for(int j=0;j<K2;j++){ Poly3Coefficient= Poly1[i].coefficient*Poly2[j].coefficient; Poly3Exponent=Poly1[i].exponent+Poly2[j].exponent; Poly3[Poly3Exponent]=Poly3[Poly3Exponent]+Poly3Coefficient; } } int count=0; vector<Polynomials> result; Polynomials item; for(int i=2000;i>=0;i--){ if(Poly3[i]!=0){ item.coefficient=Poly3[i]; item.exponent=i; result.push_back(item); count++; } } cout<<count; for(int i=0;i<count;i++){ cout<<" "<<setfill('0') <<setiosflags(ios::fixed)<<setprecision(1)<<result[i].exponent<<" "<<result[i].coefficient; } return 0; }