当前位置: 首页 > article >正文

每日刷题记录(十)

目录

  • 第一题:组队竞赛
    • 解题思路:
    • 代码实现:
  • 第二题:删除公共字符
    • 解题思路:
    • 代码实现:
  • 第三题:排序子序列
    • 解题思路:
    • 代码实现:
  • 第四题:倒置字符串
    • 解题思路:
    • 代码实现:
  • 第五题:字符串中找出连续最长的数字串
    • 解题思路:
    • 代码实现:

第一题:组队竞赛

牛牛举办了一次编程比赛,参加比赛的有3*n个选手,每个选手都有一个水平值a_i.现在要将这些选手进行组队,一共组成n个队伍,即每个队伍3人.牛牛发现队伍的水平值等于该队伍队员中第二高水平值。
例如:
一个队伍三个队员的水平值分别是3,3,3.那么队伍的水平值是3
一个队伍三个队员的水平值分别是3,2,3.那么队伍的水平值是3
一个队伍三个队员的水平值分别是1,5,2.那么队伍的水平值是2
为了让比赛更有看点,牛牛想安排队伍使所有队伍的水平值总和最大。
如样例所示:
如果牛牛把6个队员划分到两个队伍
如果方案为:
team1:{1,2,5}, team2:{5,5,8}, 这时候水平值总和为7.
而如果方案为:
team1:{2,5,8}, team2:{1,5,5}, 这时候水平值总和为10.
没有比总和为10更大的方案,所以输出10.

输入描述:

输入的第一行为一个正整数n(1 ≤ n ≤ 10^5) 第二行包括3*n个整数a_i(1 ≤ a_i ≤
10^9),表示每个参赛选手的水平值.

输出描述:

输出一个整数表示所有队伍的水平值总和最大值.

示例1

输入 2 5 2 8 5 1 5
输出 10

解题思路:

  1. 用数组接收所有水平值,用Arrays.sort方法对数组进行排序
  2. 选择水平值时使用贪心算法,选择当前情况下最大的水平值

代码实现:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
           int n  = in.nextInt();
           int[] nums = new int[3*n];
           for(int i = 0;i < (3*n);i++) {
            nums[i] = in.nextInt();
           }
           Arrays.sort(nums);
           long sum = 0;
           for(int i = 0;i < n;i++) {
            sum += nums[nums.length-(2*(i+1))]; 
           }
           System.out.println(sum);
        }
    }
}

第二题:删除公共字符

描述
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”

输入描述:
每个测试输入包含2个字符串
输出描述:
输出删除后的字符串

示例1

输入: They are students. aeiou
输出: Thy r stdnts.

解题思路:

  1. 用HashSet接收第二个字符串
  2. 遍历第一个字符串,如果set中不包含该字符,则拼接到ret上

代码实现:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) { 
            String a = in.nextLine();
            String b = in.nextLine();
            HashSet<Character> set = new HashSet<>();
            for(char ch : b.toCharArray()) {
                set.add(ch);
            }
            String ret = ""; 
            for(int i = 0; i < a.length();i++) {
                if(!set.contains(a.charAt(i))) {
                    ret +=a.charAt(i);
                } 
            }
            System.out.println(ret); 
        }
    }
}

第三题:排序子序列

牛牛定义排序子序列为一个数组中一段连续的子序列,并且这段子序列是非递增或者非递减排序的。牛牛有一个长度为n的整数数组A,他现在有一个任务是把数组A分为若干段排序子序列,牛牛想知道他最少可以把这个数组分为几段排序子序列.
如样例所示,牛牛可以把数组A划分为[1,2,3]和[2,2,1]两个排序子序列,至少需要划分为2个排序子序列,所以输出2

输入描述:
输入的第一行为一个正整数n(1 ≤ n ≤ 10^5)
第二行包括n个整数A_i(1 ≤ A_i ≤ 10^9),表示数组A的每个数字。

输出描述:
输出一个整数表示牛牛可以将A最少划分为多少段排序子序列

示例1

输入
6
1 2 3 2 2 1
输出
2

解题思路:

  1. 遍历数组,由于要比较array[i]和array[i+1],所以定义数组时将数组大小设为n+1
  2. 若array[i] > array[i+1],则进入非递增序列判断,直到遍历到下一个值不大于等于为止,count++,然后进行下一位置的判断
  3. 若a[i+1]<a[i],则进入非递增序列判断,直到遍历到下一个值不小于等于为止,count++,然后进行下一位置的判断
  4. a[i+1] == a[i]不进行操作,++i进行下一位置遍历,因为相等既可以属于非递增序列,也可以属于非递减序列。

代码实现:

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int[] array = new int[n + 1];
            for (int i = 0; i < n; i++) {
                array[i] = in.nextInt();
            }
            int count = 0;
            int i = 0;
            while (i < n) {
                if (array[i] > array[i + 1]) {
                    while (i < n && array[i] > array[i + 1]) {
                        i++;
                    }
                    count++;
                    i++;
                } else if (array[i] == array[i + 1]) {
                    i++;
                } else {
                    while (i < n && array[i] < array[i + 1]) {
                        i++;
                    }
                    count++;
                    i++;
                }
            }
            System.out.println(count);
        }
    }
}

第四题:倒置字符串

描述
将一句话的单词进行倒置,标点不倒置。比如 I like beijing. 经过函数后变为:beijing. like I
输入描述:
每个测试输入包含1个测试用例: I like beijing. 输入用例长度不超过100
输出描述:
依次输出倒置之后的字符串,以空格分割

示例1

输入: I like beijing.
输出: beijing. like I

解题思路:

  1. 构造逆置字符串函数reverse,先逆置整个字符串
  2. 遍历字符串,将每个单词逆置

代码实现:

public class Main {
    public static void reverse(char[] ch, int start, int end) {
        for (int i = 0; i < ch.length; i++) {
            if (start < end) {
                char tmp = ch[start];
                ch[start] = ch[end];
                ch[end] = tmp;
                start++;
                end--;
            }
        }
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String s = in.nextLine();
            char[] ch = s.toCharArray();
            int len = ch.length;
            reverse(ch, 0, len - 1);
            int i = 0;
            while (i < len) {
                int j = i;
                while (j < len && ch[j] !=' ') {
                    j++;
                }
                if(j < len) {
                    reverse(ch,i,j-1);
                    i = j+1;
                }else {
                    reverse(ch,i,j-1);
                    i = j;
                }
            }
            String ret = new String(ch);
            System.out.println(ret);
        }
    }
}

第五题:字符串中找出连续最长的数字串

描述
读入一个字符串str,输出字符串str中的连续最长的数字串

输入描述
个测试输入包含1个测试用例,一个字符串str,长度不超过255。

输出描述:
在一行内输出str中里连续最长的数字串。

示例1

输入: abcd12345ed125ss123456789
输出: 123456789

解题思路:

  1. 遍历字符串,用cur记录当前最长的数字串
  2. 如果遇到不是数字字符,则表示一个连续的数字串结束了,则将数字串跟之前的数字串比较,如果更长,则更新更长的数字串更新到ret
  3. 最后单独判断一下cur记录的最后一个数字串为字符串末尾的情况

代码实现:

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

http://www.kler.cn/news/9708.html

相关文章:

  • 刚入职场的年轻开发人员应该如何提高自己的技能,掌握哪些知识
  • LOTO示波器电源环路增益分析客户实测
  • Kotlin语法-Day10
  • EndNote X9 插入参考文献方法 及论文参考文献常见问题总结
  • idea无maven选项
  • 佳明手表APP开发系列01——简单汉化英文版
  • synchronized的简单理解
  • QT样式表详解
  • ChatGPT原理剖析
  • 云HIS源码:云HIS系统操作指南
  • 【数据结构】栈和队列(笔记总结)
  • PHP请求的好处,PHP如何请求淘宝开放接口
  • 代码随想录算法训练营第38天|509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
  • 20230403在WIN10下通过ffmpeg调用NVIDIA的硬件加速wmv视频转码为MP4格式
  • WEB系统页面超过一定时间就自动跳转至登录页(或退出)
  • ios jenkins配置实现iOS项目自动化打包
  • 初谈 ChatGPT
  • 【python界面编程】基于tinker界面编程加法
  • 如何降低node 包版本
  • 一文读懂:低代码开发平台对企业效益有什么作用?
  • ant design pro + umi4的动态菜单与动态路由
  • 带你玩转Python爬虫(胆小者勿进)千万别做坏事·······
  • 明明有index.jsp文件访问的时候却显示404
  • 指针C语言基础代码总结
  • 图嵌入 Node2Vec
  • 前端开发必看100道大厂面试题集锦(一)
  • 网站怎么接入chatGPT来自动写文章
  • python【反爬、xpath解析器、代理ip】
  • ZooKeeper领导者选举流程
  • 子集和问题