博客
关于我
牛客刷题组队竞速,删除公共字符,最长的数字串
阅读量: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/

    你可能感兴趣的文章
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>