笔试狂刷--Day4(滑动窗口+贪心)

大家好,我是LvZi ,今天带来笔试狂刷--Day4

一.简写单词

1.题目链接: 简写单词
2.题目分析:

一个简单的模拟

3.代码实现:

1.先整体读取,再处理

java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String[] str = s.split(" ");// 将字符串按照空格进行分割
        StringBuffer sb = new StringBuffer();
        for(String tmp : str) {
            sb.append(tmp.toUpperCase().charAt(0));
        }

        System.out.print(sb.toString());
    }
}

2.一个字符串一个字符串读取

java 复制代码
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNext()) {// 遇到空格就停止
            char ch = in.next().charAt(0);
            if(ch >= 'a' && ch <= 'z') System.out.print((char)(ch - 32));
            else System.out.print(ch);
        }
    }
}

二.dd爱框框(滑动窗⼝)

1.题目链接: dd爱框框(滑动窗⼝)
2.分析:

简单的滑动窗口

3.代码实现:

java 复制代码
import java.util.Scanner;
import java.util.*;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    
    public static void main(String[] args) throws IOException{
        int n = in.nextInt(), x = in.nextInt();
        int[] a = new int[n + 1];
        for(int i = 1; i <= n; i++) a[i] = in.nextInt();
        
        int sum = 0;
        int[] ret = new int[2];
        ret[1] = 0x3f3f3f3f;
        for(int l = 1, r = 1; r <= n; r++){
            sum += a[r];// 进窗口
            
            while(sum >= x){// 判断
                if(r - l < ret[1] - ret[0]) {
                    ret[0] = l; ret[1] = r;// 更新结果
                }

                sum -= a[l++];// 出窗口
            }
        }
        
        System.out.println(ret[0] +" " + ret[1]);
    }
}

// 本题由于输入数据过多 正常的输入会超时  需要使用快读
class 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();
    }
    
    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
}

三.除2!(贪⼼ + 堆)

1.题目链接: .除2!
2.题目分析:

使用贪心策略,每次减去偶数的最大值

代码:

java 复制代码
import java.util.Scanner;
import java.util.PriorityQueue;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), k = in.nextInt();
        PriorityQueue<Integer> q = new PriorityQueue<>((o1,o2) -> o2 - o1);// 调整为大根堆

        long sum  = 0, x;
        
        // 将所有的偶数添加进队列
        for(int i = 0; i < n; i++) {
            x = in.nextInt();
            sum += x;
            if(x % 2 == 0) q.add((int)x);
        }

        while(!q.isEmpty() && k-- != 0) {
            long t = q.poll() / 2;
            sum -= t;
            if(t % 2 == 0) {// 如果删除的数字/2 仍然是偶数 仍要添加进入队列
                q.add((int)t);
            }
        }
        
        System.out.println(sum);
    }
}
相关推荐
无声旅者1 小时前
深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
java·ide·ai·intellij-idea·ai编程·continue·openapi
Blossom.1181 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song1 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive1 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
Ryan-Joee1 小时前
Spring Boot三层架构设计模式
java·spring boot
TNTLWT1 小时前
Qt功能区:简介与安装
开发语言·qt
Hygge-star1 小时前
【数据结构】二分查找5.12
java·数据结构·程序人生·算法·学习方法
dkmilk2 小时前
Tomcat发布websocket
java·websocket·tomcat
工一木子2 小时前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis
哞哞不熬夜2 小时前
JavaEE--初识网络
java·网络·java-ee