Cattle off to prove safety net offer 45 questions - poker straight

topic:

LL particularly good mood today, because he bought a deck of cards and found that there actually has two king, two small king (the deck was originally 54 ^ _ ^) ... he randomly drawn from the five brand, wanted to Cece own luck, see if you can be able to get straight, if able to get it, he decided to buy a sports lottery, hey! ! "Red A, 3 of spades, Wang, King, square piece 5", "Oh My God!" Straight ..... LL is not happy, he thought, decides \ Amy can be seen as any number, and regarded as A 1, J is 11, Q is 12, K 13. The above five cards can become "1,2,3,4,5" (king size and 2, respectively seen as 4), "So Lucky!". LL decided to buy sports lottery friends. Now, I ask you to use this piece of card simulate the above procedure, and then tell us how LL luck, if the card can be composed of straight to output true, otherwise it will output false. For convenience, you can think of king size is 0.

Ideas:

Casual working the above-described specific information, 0-13, total 14 digits, 0 may serve any number, then enter the five numbers, judgment is not continuous straight.

So how do you analyze it? 

Obviously 0 can act as any element. The number of zeros it is only up to four;

Obviously, in addition to a conventional general condition determination array: the array length is not 5, the numerical array bounds (not between 0-13), false is returned;

Obviously, other than 0 can be repeated, other elements repeated return false;

When other elements are not repeated, the non-zero non-zero minimum value by subtracting the maximum value max min <= 4;

Why is it? Because first, a maximum of four repeated 0; Secondly, if the non-zero non-zero minimum value by subtracting the maximum value max min> = 5; then the max and min instructions between at least four elements . And under such conditions, 0 element at most three, obviously not constitute straight

Not some simple code as follows:

. 1  class Solution {
 2  public :
 . 3      BOOL IsContinuous (Vector < int > Numbers) {
 . 4          // king size 0 (4 0), remaining from 1-13 (all four); 
. 5          IF (numbers.size ( !) = 5 ) // array length is not 5, returns to false 
. 6              return  to false ;
 . 7          for ( int I = 0 ; I <numbers.size (); I ++ )
 . 8              IF (Numbers [I] < 0 || Numbers [ I]> 13 is ) // interval of values does not meet the requirements, returns to false 
. 9                  return to false ;
 10          Sort (numbers.begin (), numbers.end ()); // sort 
. 11          for ( int I = 0 ; I <numbers.size () - . 1 ; I ++ )
 12 is          {
 13 is              IF (Numbers [I] == 0 )
 14                  Continue ;
 15               IF (Numbers [I] == Numbers [I + . 1 ]) // except 0 can not be duplicated elements 
16                   return  to false ;
 . 17          }
 18 is           for ( int I = 0 ; I <Numbers. size (); i ++)
 . 19          {
 20 is              IF (Numbers [I] =! 0 )
 21 is              {
 22 is                IF (Numbers [numbers.size (-) . 1 ] -numbers [I]> = . 5 ) // MAXVALUE-MINVALUE> =. 5 flase returned (because after sorting) 
23 is                    return  to false ;
 24               the else 
25                   return  to true ;
 26 is              }
 27          }
 28          
29                   
30      }
 31 is };

 

Guess you like

Origin www.cnblogs.com/shaonianpi/p/12554910.html