Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Solution {
- def dec2bin(n: Int): String = {
- if (n / 2 == 0) return (n % 2).toString
- if (n % 2 == 0) return dec2bin(n/2) + "0"
- else return dec2bin(n/2) + "1"
- }
- def findConsecOnes(s: String): Int = {
- if (s.isEmpty) return 0
- if (s.head == '1') return findConsecOnes(s.tail) + 1
- if (s.head == '0') return findConsecOnes(s.tail) - 1
- return 0
- }
- def main(args: Array[String]) {
- val sc = new java.util.Scanner (System.in);
- var n = sc.nextInt();
- println(findConsecOnes(dec2bin(n)))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement