笔试狂刷--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);
    }
}
相关推荐
艾伦~耶格尔23 分钟前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
man201727 分钟前
基于spring boot的篮球论坛系统
java·spring boot·后端
2401_8581205344 分钟前
Spring Boot框架下的大学生就业招聘平台
java·开发语言
S hh44 分钟前
【Linux】进程地址空间
java·linux·运维·服务器·学习
转调1 小时前
每日一练:地下城游戏
开发语言·c++·算法·leetcode
Java探秘者1 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
攸攸太上1 小时前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
2301_786964361 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
2303_812044461 小时前
Bean,看到P188没看了与maven
java·开发语言
苹果醋31 小时前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx