1002. A+B for Polynomials (25)

 

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

This 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.

  1 #include <iostream>

  2 

  3 #include <iomanip>

  4 

  5 using namespace std;

  6 

  7  

  8 

  9 struct fun

 10 

 11 {

 12 

 13    bool ifdo;

 14 

 15    double sum;

 16 

 17 };

 18 

 19  

 20 

 21 int main()

 22 

 23 {

 24 

 25  

 26 

 27 int n1;

 28 

 29 while(cin>>n1)

 30 

 31 {

 32 

 33   fun F[1001];

 34 

 35   int i,num,n2;

 36 

 37   double val;

 38 

 39          for(i=0;i<=1000;i++)

 40 

 41 {

 42 

 43      F[i].ifdo=false;

 44 

 45               F[i].sum=0.0;

 46 

 47 }

 48 

 49  

 50 

 51   for(i=0;i<n1;i++)

 52 

 53   {

 54 

 55      cin>>num>>val;

 56 

 57  F[num].ifdo=true;

 58 

 59           F[num].sum+=val;

 60 

 61   }

 62 

 63        

 64 

 65   cin>>n2;

 66 

 67  

 68 

 69        for(i=0;i<n2;i++)

 70 

 71   {

 72 

 73      cin>>num>>val;

 74 

 75  F[num].ifdo=true;

 76 

 77           F[num].sum+=val;

 78 

 79   }

 80 

 81  

 82 

 83   num=0;

 84 

 85    for(i=0;i<=1000;i++)

 86 

 87 {

 88 

 89    if(F[i].ifdo&&F[i].sum!=0)  num++;

 90 

 91 }

 92 

 93  

 94 

 95 cout<<num;

 96 

 97         

 98 

 99 for(i=1000;i>=0;i--)

100 

101 {

102 

103    if(F[i].ifdo&&F[i].sum!=0)  

104 

105 {

106 

107               

108 

109   cout<<" "<<i<<" "<<fixed<<setprecision(1)<<F[i].sum;

110 

111 }

112 

113 }

114 

115  

116 

117 cout<<endl;

118 

119 }

120 

121    return 0;

122 

123  

124 

125 }

 

你可能感兴趣的:(for)