题目描述
blablabla
样例
blablabla
算法1
$O(n^logn)$
返回n的最后一位1:lowbit(n) = n & -n
-n = ~n + 1 //(x取反加1)
Java 代码
import java.util.*;
import java.io.*;
public class Main {
final static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
final static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
String[] str = reader.readLine().split("\\s+");
int n = Integer.parseInt(str[0]);
int[] a = new int[n];
str = reader.readLine().split("\\s+");
for(int i = 0; i < n; i++){
int val = Integer.parseInt(str[i]);
System.out.print(findOne(val) + " ");
}
}
private static int findOne(int val){
int res = 0;
int k = 1;
while(val > 0){
val -= lowbit(val);
res ++;
}
return res;
}
public static int lowbit(int x){
return x & -x;
}
}