当前位置: 首页 > article >正文

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);
    }


http://www.kler.cn/news/338926.html

相关文章:

  • LeetCode讲解篇之322. 零钱兑换
  • 每天五分钟深度学习pytorch:基于pytorch搭建一元线性回归模型
  • Axure中文版:原型设计新手必备工具,轻松上手!
  • 数据挖掘笔记part one (认识数据挖掘)
  • matplotlib中文显示乱码问题
  • 线性代数书中求解齐次线性方程组、非齐次线性方程组方法的特点和缺陷(附实例讲解)
  • 在 JavaScript 中使用 window 对象来刷新页面
  • 深入浅出:现代JavaScript开发者必知必会的Web性能优化技巧
  • 体系结构论文(五十四):Reliability-Aware Runahead 【22‘ HPCA】
  • 简单认识redis-5 jdbc 与 jedis 使用的区别
  • Redis 缓存策略详解:提升性能的四种常见模式
  • springboot整合mybatis案例
  • Spring Boot助力医院数据管理
  • 【C语言】关于指针各项细节以及与其他知识点关联
  • Java Web 开发
  • 目标检测 DN-DETR(2022)
  • 数据库分区
  • tensorflow快速入门--如何定义张量、定义网络结构、超参数设置、模型训练???
  • 程序设计基础I-实验5 一维数组
  • XTuner微调个人小助手认知