daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. object Solution {
  2.  
  3.     def dec2bin(n: Int): String = {
  4.         if (n / 2 == 0) return (n % 2).toString
  5.         if (n % 2 == 0) return dec2bin(n/2) + "0"
  6.         else return dec2bin(n/2) + "1"
  7.     }
  8.    
  9.     def findConsecOnes(s: String): Int = {
  10.         if (s.isEmpty) return 0
  11.         if (s.head == '1') return findConsecOnes(s.tail) + 1
  12.         if (s.head == '0') return findConsecOnes(s.tail) - 1
  13.         return 0
  14.     }
  15.    
  16.     def main(args: Array[String]) {
  17.         val sc = new java.util.Scanner (System.in);
  18.         var n = sc.nextInt();
  19.         println(findConsecOnes(dec2bin(n)))
  20.     }
  21. }
RAW Paste Data
Top