博客
关于我
牛客刷题组队竞速,删除公共字符,最长的数字串
阅读量:588 次
发布时间:2019-03-11

本文共 2498 字,大约阅读时间需要 8 分钟。

组队竞速

牛牛举办了一场编程比赛,共有3*n个选手参加。每个选手都有一个水平值a_i。牛牛的目标是将选手分成n组,每组3人,要求每组的水平值等于该组队员中第二高的水平值。牛牛希望通过合理分组,使得所有队伍的水平值总和最大化。

解题思路

为了最大化所有队伍的水平值总和,每个队伍的第二个值应尽可能大。因此,合理的策略是将选手按水平值从小到大排序后,将最大的n个水平值放在右边的两端,中间的n个水平值放在左边。这样可以确保中间的水平值尽量大,进而使得每个队伍的第二个值最大。

解决代码

import java.util.*;public class Main{    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        int n = scan.nextInt();        int[] array = new int[3 * n];        for (int i = 0; i < 3 * n; i++) {            array[i] = scan.nextInt();        }        Arrays.sort(array);        long sum = 0;        for (int i = 1; i < 3 * n; i++) {            if (i % 4 == 0) {                if (i == 3 * n - 1) {                    sum += array[i];                }            } else {                sum += array[i];            }        }        System.out.println(sum);    }}

删除公共字符

输入两个字符串str1和str2,从str1中删除str2中所有的字符。例如,输入“They are students.”和“aeiou”,则删除后的字符串变为“Thy r stdnts.”。

解决方法

  • 首先,遍历str2,将每个字符存储在集合中。
  • 然后,遍历str1,检查每个字符是否存在于集合中,如果不存在,则保留该字符。
  • 将处理后的字符串输出。
  • 解决代码

    import java.util.*;public class Main{    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        String str1 = scan.nextLine();        String str2 = scan.nextLine();        Map
    map = new HashMap<>(); for (int i = 0; i < str2.length(); i++) { map.put(str2.charAt(i), i); } StringBuilder sb = new StringBuilder(str1); for (int i = 0; i < sb.length(); i++) { if (!map.containsKey(sb.charAt(i))) { char c = sb.charAt(i); sb.deleteCharAt(i); } } System.out.println(sb.toString()); }}

    最长的数字串

    读取一个字符串str,找出其中连续最长的数字串。

    解决方法

  • 初始化两个字符串变量curret,分别用于记录当前和最长的数字串。
  • 遍历输入字符串中的每一个字符:
    • 如果字符是数字,则添加到当前字符串cur中。
    • 如果不是数字,则比较curret的长度,保留较长的字符串,并重置cur为空。
  • 遍历结束后,比较curret,保留最长的数字串。
  • 输出最长数字串。
  • 解决代码

    import java.util.*;public class Main{    public static void main(String[] args) {        Scanner scan = new Scanner(System.in);        String str = scan.nextLine();        String cur = "";        String ret = "";        for (int i = 0; i < str.length(); i++) {            char ch = str.charAt(i);            if (ch >= '0' && ch <= '9') {                cur += ch;            } else {                if (cur.length() > ret.length()) {                    ret = cur;                }                cur = "";            }        }        if (cur.length() > ret.length()) {            ret = cur;        }        System.out.println(ret);    }}

    转载地址:http://qsstz.baihongyu.com/

    你可能感兴趣的文章
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm上传自己的项目
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>