笔试狂刷--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 小时前
Java-servlet(完结篇)过滤器乱码解决与监听器
java·运维·服务器·hive·servlet·jsp
Blood_J2 小时前
python网络爬虫
开发语言·爬虫·python
小开不是小可爱2 小时前
leetcode_383. 赎金信_java
java·数据结构·算法·leetcode
xiaowu0803 小时前
C# task任务异步编程提高UI的响应性
开发语言·c#
kill bert5 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
低头专研6 小时前
Markdown标题序号处理工具——用 C 语言实现
c语言·开发语言·typora·markdown文件标题编号·md文件标题序号
刚入门的大一新生8 小时前
C++初阶-C++入门基础
开发语言·c++
你是理想8 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
helloworld工程师8 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop8 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea