选择题
编程题
题目1
import java.util.Scanner;
public class Main {
public static String TentoTwo(int n) {
StringBuffer sum = new StringBuffer();
while (n != 0) {
sum.append(n % 2);
n /= 2;
}
return sum.reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//十进制转换为二进制
String s = TentoTwo(n);
int res = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
res++;
} else {
max = Math.max(max, res);
res = 0;
}
}
System.out.println(Math.max(max, res));
sc.close();
}
}
import java.util.Scanner;
public class Main {
public static String TentoTwo(int n) {
StringBuffer sum = new StringBuffer();
while (n != 0) {
sum.append(n % 2);
n /= 2;
}
return sum.reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//十进制转换为二进制
String s = TentoTwo(n);
int res = 0;
int max = Integer.MIN_VALUE;
//全是1:7
//最后出现连续多个1:87
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1') {
res++;
max = Math.max(max, res);
} else {
res = 0;
}
}
System.out.println(max);
sc.close();
}
}
题目2
import java.util.*;
public class LCA {
public int getLCA(int a, int b) {
if(a == b)
return a;
//9 7
//4 7
//4 3
//2 3
//2 1
//1 1
while(a!=b){
//要判断大小之后再去除等2
if(a>b)
a/=2;
else
b/=2;
}
return a;
}
}