编程题
查找输入整数二进制中1的个数
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
int count = 0;
while(n!=0){
if(n%2==1){
count++;
}
n = n/2;
}
System.out.println(count);
}
}
}
手套
@SuppressWarnings({"all"})
public class Main {
public int findMinimum(int n, int[] left, int[] right) {
int leftSum = 0;
int rightSum = 0;
int leftMin = Integer.MAX_VALUE;
int rightMin = Integer.MAX_VALUE;
int sum = 0;
for (int i = 0; i < n; i++) {
if (left[i] * right[i] == 0) {
sum += left[i] + right[i];
} else {
leftSum += left[i];
rightSum += right[i];
if (leftMin > left[i]) {
leftMin = left[i];
}
if (rightMin > right[i]) {
rightMin = right[i];
}
}
}
int min = Math.min((leftSum - leftMin + 1), (rightSum - rightMin + 1));
return sum + min + 1;
}
}