URI Solution 1593 Binary Function - Solution in java | Ad Hoc
URI Online Judge Solution 1593 Binary Function| Ad Hoc
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1593
Problem Name: URI Problem 1593 Binary Function
Problem Number : URI Problem 1593 Binary Function Solution
Online Judge : URI Online Judge Solution
Level: Ad Hoc
Solution Language : java
URI Solution 1593 Binary Function Code in java:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public static int countOccurrences(String haystack, char needle) { int count = 0; for (int i=0; i < haystack.length(); i++) { if (haystack.charAt(i) == needle){ count++; } } return count; } public static void main(String[] args) throws IOException { InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir); int t, res; String bin; t = Integer.parseInt(in.readLine()); for (int i = 0; i < t; ++i) { BigInteger bg = new BigInteger(in.readLine()); bin = bg.toString(2); res = countOccurrences(bin, '1'); System.out.println(res); } } }
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks