删数、n个数里最小的k个

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;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值