1.Test.main() 函数执行后的输出是(D)
public class Test {
public static void main(String[] args) {
System.out.println(new B().getValue());
}
static class A {
protected int value;
public A(int v) {
setValue(v);//本来应该执行父类的setValue方法
// 但是子类覆写父类的方法,所以此时执行子类的setValue方法
//value = 10
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
try {
value++;//11 17
return value;//11 22 17 34
} catch (Exception e) {
System.out.println(e.toString());
} finally {
this.setValue(value);//22
System.out.println(value);//22 34
}
return value;
}
}
static class B extends A {
public B() {
super(5);
setValue(getValue() - 3);//11-3=8
}
public void setValue(int value) {
super.setValue(2 * value);//10 16
}
}
}
A.11 17 34
B.22 74 74
C.6 7 7
D.22 34 17
2.删数
public class Test11 {
private static int Solution(int[] array){
Queue<Integer> queue = new LinkedList<>();
for(int i = 0;i<array.length;i++){
queue.add(array[i]);
}
while (queue.size()!=1&&queue.size()!=0){
int count = 2;
while (count!=0){
int p = queue.peek();
queue.poll();
queue.add(p);
count--;
}
queue.poll();
}
int result = 0;
if(queue.size()!=0){
result = queue.peek();
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int[] array = new int[count];
for(int i = 0;i<array.length;i++){
array[i] = i;
}
int result = Solution(array);
System.out.println(result);
}
}//没有通过全部的测试用例
//方法二
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
if (n > 1000) {
n = 999;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
list.add(i);
}
int i = 0;
while (list.size() > 1) {
i = (i + 2) % list.size();
list.remove(i);
}
System.out.println(list.get(0));
}
}
}
3.n个数里最小的k个
public class Test12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] value = line.split(" ");
int[] num = new int[value.length-1];
for(int i = 0;i<value.length-1;i++){
int a = Integer.parseInt(value[i]);
num[i] = a;
}
int k = Integer.parseInt(value[value.length-1]);
Arrays.sort(num);
for(int i = 0;i<k;i++){
System.out.print(num[i]+" ");
}
}
}
//方法二
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
String[] s = str.split(" ");
int[] num = new int[s.length - 1];
for (int i = 0; i < s.length - 1; i++) {
num[i] = Integer.parseInt(s[i]);
}
int k = Integer.parseInt(s[s.length - 1]);
int start = 0;
int end = num.length - 1;
int index = qSort(num, start, end);
while (index != k) {
if (index > k) {
end = index - 1;
index = qSort(num, start, end);
} else {
start = index + 1;
index = qSort(num, start, end);
}
}
Arrays.sort(num, 0, k);
for (int i = 0; i < k; i++) {
System.out.print(i == k - 1 ? num[i] : num[i] + " ");
}
}
}
public static int qSort(int[] num, int start, int end) {
int dig = num[start];
while (start < end) {
while (start < end && num[end] >= dig) end--;
num[start] = num[end];
while (start < end && num[start] < dig) start++;
num[end] = num[start];
}
num[start] = dig;
return start;
}
}