Advertisement
Fundamentalen

CountOfEqualBitPairs

May 12th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class CountOfEqualBitPairs {
  5.  
  6.     public static void main(String[] args) {
  7.        
  8.         Scanner input = new Scanner(System.in);
  9.        
  10.         int number = input.nextInt();
  11.         int zeroCount = 0;
  12.         int oneCount = 0;
  13.         int pairs = 0;
  14.        
  15.         while (number != 0) {
  16.             int digit = number % 2;
  17.            
  18.             if (digit == 0) {
  19.                 zeroCount++;
  20.                
  21.                 if (zeroCount >= 2) {
  22.                     pairs++;
  23.                     zeroCount--;
  24.                 }
  25.                
  26.                 oneCount = 0;
  27.             }
  28.            
  29.             else if (digit == 1) {
  30.                 oneCount++;
  31.                
  32.                 if (oneCount >= 2) {
  33.                     pairs++;
  34.                     oneCount--;
  35.                 }
  36.                
  37.                 zeroCount = 0;
  38.             }
  39.            
  40.             number /= 2;
  41.         }
  42.        
  43.         System.out.println(pairs);
  44.        
  45.         input.close();
  46.  
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement