Talk about "don't repeat number greater than the specified minimum" problem of p

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  For “ &ldquo positive integer greater than the minimum specified; not repeat number ” ”, initially, in the algorithm: Specifies the number of large and asked for more than the minimum “ not repeat number ”   efficient implementation problems;, I give a recursive method, then a non recursive formulation is given in the same post.

  Later in the Alexia (Minmin) user code comments and on &ldquo than the specified number; for large and minimum ‘ not repeat number ’ ”   code; in my writing is explained in greater detail, and an important improvement. For larger.

  In the evaluation of playerc users "demand than the specified number is large and the minimum “ not repeat number ”   problem" in comments on the playerc method.

  The idea of playerc is no problem, but the code is complex and there are errors. Now I give my writing in this way.

Analysis and solution process in detail, describes not in the text. A detailed code notes, in the previous post is also described. The main point of the different writing and the front is realized, one plus 0101…….  

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define SIZE(x) (sizeof (x) / sizeof (x)[0])
  5 #define BE_DIG(x) ( '0' <= x && x <= '9' ) 
  6 #define MAX 128  
  7 
  8 typedef  struct
  9          { 
 10             unsigned char digs[ MAX + 1 ] ;//From low to high storage digits 
 11                                            //And set aside in a position to carry possible  
 12             unsigned char * p_high ;       //Record the top digit position 
 13          }  
 14 Number ; 
 15   
 16 void input( Number * ); 
 17 void reverse( unsigned char * , unsigned char * );
 18 void exchange( unsigned char * , unsigned char * ); 
 19 void squeeze( unsigned char * , unsigned char * * );
 20 void output( const unsigned char * , const unsigned char * ) ;  
 21 void seek( unsigned char * , unsigned char * * );
 22 void add_1( unsigned char * , unsigned char * * );
 23 unsigned char * search ( unsigned char * , unsigned char * );
 24 
 25 int main( void ) 
 26 {
 27    Number num ;
 28    
 29    input( & num );                    //Enter a positive integer
 30    seek( num.digs , & num.p_high );   //Please don't repeat number
 31    output( num.p_high , num.digs );   //Output
 32     
 33    return 0; 
 34 } 
 35 
 36 /*
 37   Enter the number to the p_n -> digs array, p_high record high position 
 38 */
 39 void input( Number * p_n )
 40 {
 41    int c ;
 42    size_t max = SIZE(p_n -> digs) - 1u ;        //Reservation carry position, enter a maximum number of digits 
 43    
 44    p_n -> p_high = p_n -> digs ;
 45    
 46    while ( c = getchar() , BE_DIG( c ) && max -- > 0u ) 
 47       * p_n -> p_high ++ = c - '0' ; 
 48    
 49    if( p_n -> p_high == p_n -> digs )               //No input digital
 50       exit( 1 );
 51    
 52    -- p_n -> p_high ;
 53    reverse( p_n -> digs ,   p_n -> p_high );       //In reverse order
 54    squeeze( p_n -> digs , & p_n -> p_high );       //Remove the first 0 
 55 }
 56 
 57 /*
 58   From the p_b point to the digital reverse p_e refers to the arrangement. 123 -> 321 
 59 */
 60 void reverse( unsigned char * p_b , unsigned char * p_e )
 61 { 
 62    while ( p_e > p_b )
 63       exchange( p_b ++ , p_e -- );
 64 } 
 65 
 66 void exchange( unsigned char * p1 , unsigned char * p2 ) 
 67 { 
 68    unsigned char c = * p1 ; 
 69    * p1 = * p2 ; 
 70    * p2 = c ; 
 71 } 
 72 
 73 /*
 74   Remove excess high 0. 00123 -> 123 
 75 */
 76 void squeeze( unsigned char * p_b , unsigned char * * pp_e )
 77 { 
 78    while ( * pp_e > p_b && * * pp_e == 0 )
 79       -- * pp_e ;
 80 } 
 81 
 82 /*
 83    * P_l points to the low, * pp_h to the most high
 84    
 85    (A)The first and most left plus 1           (left) 2 2 1 1 => 3 2 1 1  
 86    From high to low to find duplicate 32 (1) 1
 87    A new interval                 (left)(1) 1
 88    Go to the(A)                    3 2 (2) 1
 89    (2) No repeat number 1, the end of a cycle
 90    
 91    From left-1 to p_l 010101……    
 92 */ 
 93 void seek( unsigned char * p_l , unsigned char * * pp_h )
 94 {
 95    unsigned char * break_p = p_l ;
 96    unsigned char * left ;      
 97    
 98    do
 99    {
100       left = break_p ;          
101       add_1( left , pp_h );                         //[left , *pp_h]Interval left most bit plus 1
102    }
103    while ( ( break_p = search ( left , * pp_h ) ) != NULL );//NULL: cannot find the repeat numbers 
104    
105    //From left-1 to p_l 010101…… 
106    while ( left > p_l )
107    {
108       left[-1] = ! * left ;
109       left -- ; 
110    }
111 }
112 
113 /*
114   Plus 1. Treatment may carry, and change the pp_h to record the highest possible position pointer  
115 */ 
116 void add_1( unsigned char * p_l , unsigned char * * pp_h )
117 {
118    ++ * p_l ;                                     //Plus 1 lows
119    while ( p_l <* pp_h )                         //Carry handle
120    {
121       * ( p_l + 1 ) += * p_l / 10 ;
122       * p_l ++ %= 10 ; 
123    }
124    if ( * * pp_h > 9 )                            //Most places are binary 
125    {
126       * ( * pp_h + 1 ) = * * pp_h / 10 ;
127       * (* pp_h) ++  %= 10 ;
128    }
129 }
130 
131 /*
132   Search the p_h to the p_l refers to the first repeat numbers in position, if no NULL is returned 
133 */ 
134 unsigned char * search ( unsigned char * p_l , unsigned char * p_h )
135 {
136    if ( p_h == p_l )      //A number of cases 
137       return NULL ;
138 
139    while ( p_l + 1 < p_h )                        
140    {
141       if ( * p_h ==  p_h[-1] )
142          return p_h - 1 ;
143       p_h -- ;
144    }
145    
146    if ( * p_l ==  p_l[1] )
147       return p_l ;
148 
149    return NULL ;      
150 }
151 
152 /*
153   Output p_h point to point to a p_l digital digital 
154 */
155 void output( const unsigned char * p_h , const unsigned char * p_l )
156 { 
157    while ( p_h > p_l )
158       printf( "%u" , * p_h -- );
159 
160    printf( "%u" , * p_l );
161    putchar('\n');
162 } 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Roy at November 14, 2013 - 11:49 AM