PAT 1010 Grade stepped pit recording (binary search) --10 test point has not been updated to be

Topic analysis:

First, this question has many pits, I am still there on 10 test points did not pass after the finish, and the code is not written in beautiful relatively lengthy do not spray, this blog is used to record a few notes to write this question

1. hexadecimal number with respect to two different size than the commonly used two numbers after the decimal are converted to compare the size (hereinafter collectively known as a binary number N1, the unknown binary number N2)

2. While the two numbers are only 10 bits, and each bit of the number from '0' ~ 'z', respectively represent 0 to 35, but this does not mean that the value of this problem is the range of 2 to 36 hex , radix is ​​entirely possible big, big to long long, write '0' ~ 'z' just to get the results calculated by a relatively small number, and confused about

3. Since the binary radix there may be large, so to find ways of using binary choices were binary judgment, otherwise it will timeout

4. In the same time for each check out the binary radix attempt to calculate the corresponding unknown binary number N2 of the process shown in the decimal range of possible overflow of long long (becomes negative), it is noted

5. For the binary number N1 known in decimal representation is obtained when it is also possible overflow long long (this is rather strange, because if even known hex decimal representation are not recorded N1 how to go down that both N2 is calculated by comparing it 10 decimal)

6. For a binary search when the binary N2, N2 is the lower bound for the maximum number +1 N2 (example: the maximum number of 10020 is 2, so the lowest band of 3 hexadecimal), and the upper bound of N2 compared with 10 hexadecimal +1 N1 (this is a point card, here is the process of reasoning)

If the number N2 of more than one (and when more than two), this time as long as the number 1 position greater than or equal to 1, the hexadecimal if N2 is 10 hexadecimal +1 N1 and N2 is always greater than N1, then big is not necessary

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 
  5 string n1, n2;
  6 int tag;
  7 long long radix;
  8 int len1, len2;
  9 int a[15];
 10 int b[15];
 11 long long Min, Max;
 12 int judge; 
 13 
 14 void init_ab(){
 15     len1 = n1.size();
 16     len2 = n2.size();
 17     int cnt = 0;
 18     for(int i = len1-1; i >= 0; i--){
 19         if(n1[i] >= '0' && n1[i] <= '9'){
 20             a[cnt++] = n1[i] - '0';
 21         }else{
 22             a[cnt++] = n1[i] - 'a' + 10;
23          }
 24      }    
 25      // because the presence of certain unknown binary number (b), it is possible to find the way about the lower bound of the unknown binary numbers hexadecimal 
26 is      Min = 0 ; 
 27      CNT = 0 ;
 28      for ( int I = len2- . 1 ; I> = 0 ; i-- ) {
 29          IF ([I] N2> = ' 0 ' && N2 [I] <= ' . 9 ' ) {
 30              B [CNT] = N2 [I] - ' 0 ' ;
 31 is              IF (B [CNT]> Min) Min = B [CNT];
 32             CNT ++ ; 
 33 is          } the else {
 34 is              B [CNT] = N2 [I] - ' A ' + 10 ;
 35              IF (B [CNT]> Min) Min = B [CNT];
 36              CNT ++ ;
 37 [          }
 38 is      }
 39      // Min all unknown bit of the maximum number of binary + 1'd 
40      Min ++ ;
 41 is  }
 42 is  
43 is  void binary_search () {
 44 is      // first be noted that the size Min and Max Min to ensure less Max 
45      IF (Min>Max) {
 46 is          Long  Long T = Min;
 47          Min = Max;
 48          max = T;
 49      }
 50      Long  Long left = Min;
 51 is      Long  Long right = max;
 52 is      int In Flag;
 53 is      the while (left <= right) {
 54 is          Long  Long mid = (left + right) / 2 ;
 55          // number unknown mid-ary calculation of hexadecimal 10 to hexadecimal 10 hexadecimal known whether the decimal equivalent 
56 is          Long  Long ANS = 0;
 57         long long base;
 58         for(int i = 0; i < len2; i++){
 59             if(i == 0){
 60                 base = 1;
 61                 ans += base * b[i];
 62             }else{
 63                 base *= mid;
 64                 if(base < 0){
 65                     flag = 1;
 66                     BREAK ;
 67                  }
 68                  ANS + = Base * B [I];
 69              }
 70              // Analyzing overflow or has greater than 10 known decimal number hexadecimal 
71 is              IF (ANS> Max - . 1 || ANS < 0 ) {
 72                  In Flag = . 1 ;
 73 is                  BREAK ;
 74              }
 75          } 
 76          IF (ANS <Max - . 1 ) In Flag = - . 1 ;
 77          IF (ANS Max == - . 1 ) In Flag =0;
 78         if(flag == 0){
 79             cout<<mid<<endl;
 80             break;
 81         }else if(flag == -1){
 82             left = mid + 1;
 83         }else right = mid - 1;
 84     }
 85     if(left > right) cout<<"Impossible"<<endl;    
 86 } 
 87 
 88 void cal_Max(){
89      Long  Long  Base ;
 90      // where Max is set to a known binary numbers decimal notation + 1, and the default does not exceed longlong range, it is too complicated or the subject 
91 is      Max = 0 ;
 92      for ( int I = 0 ; I <LEN1; I ++ ) {
 93          IF (I == 0 ) {
 94              Base = . 1 ;
 95              Max + = Base * A [I];
 96          } the else {
 97              Base * = the radix;
 98              Max = + Base* A [I];
 99          }
 100          IF (Max < 0 ) {         // known binary number equal to n1 n2 has overflowed unless otherwise directly Impossible? 
101              Judge = . 1 ;
 102              IF (N1 == N2) COUT the radix << << endl;
 103              the else COUT << " Impossible " << endl;
 104              BREAK ;
 105          }
 106      }
 107      Max ++ ; 
 108  }
 109  
110  int main ( ) {
 111      the while(CIN Tag >> n1 >> >> >> N2 the radix) {
 112          Judge = 0 ;
 113          IF (Tag == 2 ) {     // always known n1 for storing binary number 
114              String T = n1;
 115              n1 = n2;
 1 16              n2 = T; 
 117          }
 1 18          // the n1 and n2 each bit of two numbers stored in the array ab 
119          init_ab ();
 120          // the unknown binary hexadecimal number calculated sector 
121          cal_Max (); 
 122          IF (Judge == 0 ) {
 123              //Since the binary search may need to deal with overflows too large binary calculation between the upper and lower bounds 
124              binary_search (); 
 125          }
 126      }
 127      return  0 ;
 128 }

 

Guess you like

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