-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBits.java
49 lines (38 loc) · 960 Bytes
/
Bits.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package programming_contest_problems;
import java.util.*;
import java.io.*;
public class Bits{
public static void main(String [] args)
{
Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int iter = in.nextInt();
in.nextLine();
for (int i= 0; i< iter; i++ )
{
String bitString = Integer.toString(in.nextInt());
String [] arr = bitString.split("");
int longest = 0;
int longesttemp = 0;
String bit2;
String test = "";
for (int j = 0; j< arr.length; j++)
{
test = test + arr[j];
bit2 = Integer.toBinaryString(Integer.parseInt(test));
char [] charArray = bit2.toCharArray();
longesttemp = 0;
for (int c = 0; c<charArray.length; c++){
if (Character.compare(charArray[c], '1') ==0)
{
longesttemp++;
}
}
if (longest <longesttemp)
{
longest = longesttemp;
}
}
System.out.println(longest);
}
}
}