自学内容网 自学内容网

day03 笔试练习

1.简写单词

题目链接:简写单词_牛客题霸_牛客网

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){ // 输入多少读入多少
            char ch = sc.next().charAt(0); // 提取首字母
            if(ch >= 'a' && ch <= 'z'){
                System.out.print((char) (ch - 32));
            }else {
                System.out.print(ch);
            }
        }
    }

 2.dd爱框框

题目链接:登录—专业IT笔试面试备考平台_牛客网

解题思路:

注意:题目所给n个数字是从 a[1] 开始读入

我们创建两个变量 left,right,都从1下标位置开始记录,right一直向后移动直到相加的结果大于等于x的时候开始更新 retLeft 和 retRight 下标(retLeft 和 retRight指的是相加结果大于等于sum目前最小区间下标)

同时left下标向右移动一位,right在之前的位置上继续右移,因为输入的数字都是大于1的数字所以right没必要从left新下标开始相加,left右移sum只会减少,而right下标是第一次出现sum相加大于等于x的下标,所以前面的数字相加肯定是小于x的

只要right和left下标上的数字相加大于等于x并且长度比之前小的时候retLeft 和 retRight 就开始更新下标以此循环直到找出最小长度

public static void main(String[] args) throws IOException{
        Read read = new Read();
       int n = read.nextInt();
       int x = read.nextInt();
       int[] arr = new int[n + 1];
       int left = 1,right = 1;
       int sum = 0,retLeft = -1 , retRight = -1,retLen = n;
        for (int i = 1; i < n + 1; i++) {

            arr[i] = read.nextInt();

        }

        while(right <= n){
            //进窗口
            sum += arr[right];
            while(sum >= x){
                if(right - left + 1 < retLen){

                    retLeft = left;
                    retRight = right;
                    retLen = right - left + 1;

                }
                sum -= arr[left++];
            }
           right++;
        }
        System.out.println(retLeft +" " + retRight);
    }
    static class Read // ⾃定义快读 Read 防止栈溢出
    {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String next() throws IOException
        {
            while(!st.hasMoreTokens())
            {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        String nextLine() throws IOException {
        return bf.readLine();
    }

        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }

        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }

3.除2!(贪心+堆)

题目链接:登录—专业IT笔试面试备考平台_牛客网

 解题思路:

创建大根堆把偶数放进去,最大的偶数一直除二得到合就为最小

  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(),k = sc.nextInt();
        PriorityQueue<Integer> heap = new PriorityQueue<>((a,b) ->{
            return b -a;
        });
        long sum = 0,x;
        for (int i = 0; i < n; i++) {
              x = sc.nextLong();
              sum+=x;
              if(x % 2 == 0){
                  heap.add((int)x);
              }
        }
        while(!heap.isEmpty() && k-- != 0){
            long t = heap.poll()/2;
            sum -=t;
            if(t % 2 == 0){
                heap.add((int)t);
            }
        }
        System.out.println(sum);
    }


原文地址:https://blog.csdn.net/2301_80785428/article/details/142694366

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!