PAT 1009 Grade drifting across the water problem

Topic Analysis: Simple analog multiplication of polynomials, you can assume that the unknowns x, exp is the exponential of x, coe is the coefficient of x, it is easy to answer, extrapolated, note that the answer is from high to low output index, while Note that this is a polynomial multiplication, although the scope of the index only 0 to 1000, but the index range multiplication result is 0 to 2000, aware that the array defined range, open a small program that creates a segmentation fault (subscript out of bounds, visit should access memory)

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  struct the Node {
 . 5      int exp;         // index 
. 6      Double COE;         // coefficient 
. 7 } K [ 2005 ], A [ 15 ], B [ 15 ];         // for i.e. an array subscript k is the index corresponding to the array so that for k exp is in fact superfluous 
 . 8  
. 9  // initialize an array of structures 
10  void the init () {
 . 11      for ( int I = . 1 ; I <= 10 ; I ++ ) {
12         a[i].exp = 0;
13         a[i].coe = 0;
14         b[i].exp = 0;
15         b[i].coe = 0;
16     } 
17     for(int i = 0; i <= 2000; i++){
18         k[i].exp = 0;
19         k[i].coe = 0;
20     }
21 }
22 
23 int main(){
24     int n1, n2;
25     while(scanf("%d", &n1) != EOF){
26         init();
27         for(int i = 1; i <= n1; i++){
28             scanf("%d%lf", &a[i].exp, &a[i].coe);
29         }
30         scanf("%d", &n2);
31         for(int i = 1; i <= n2; i++){
32             scanf("%d%lf", & B [I] .exp, & B [I] .coe);
 33 is          }
 34 is          for ( int I = . 1 ; I <= N1; I ++ ) {
 35              for ( int J = . 1 ; J <= N2; J ++ ) {
 36                  // for each multiplying a array with each bit b of the array, the result is stored in the array k
 37                  // get index 
38 is                  int exp = a [I] + .exp b [J] .exp;
 39                  // get coefficient 
40                  Double COE = A [I] .coe * B [J] .coe;
 41 is                  K [exp] + = .coe COE; 
 42 is              }
 43 is         }
 44          // count the number of items is again significant (coefficient is not 0) 
45          int NUM = 0 ;
 46 is          for ( int I = 2000 ; I> = 0 ; i-- ) {
 47              IF (K [I] ! .coe = 0 ) NUM ++ ;
 48          } 
 49          the printf ( " % D " , NUM);
 50          // coefficient output from high to low are not 0 in the case where a polynomial of each of the 
51 is          for ( int I = 2000 ; I> = 0 ; i-- ) {
 52 is              IF(k[i].coe != 0){
53                 printf(" %d %0.1lf", i, k[i].coe);
54             }
55         } 
56         printf("\n");
57     }
58     return 0;
59 } 

 

Guess you like

Origin www.cnblogs.com/findview/p/11646645.html