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

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

组队竞速

题目:

牛牛举办了一次编程比赛,参加比赛的有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.

题解:

队伍的水平值等于该队伍队员中第二高水平值,为了所有队伍的水平值总和最大的解法,也就是说每个队伍的第二个值是尽可能大的值。所以实际值把最大值放到最右边,最小是放到最左边。

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];        long sum=0;        for(int i=0;i<3*n;i++){           array[i]=scan.nextInt();        }        Arrays.sort(array);        for(int i=0;i

删除公共字符

题目:

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

题解:

1.遍历str2这个字符串,把对应的字符放到hash[]中
2.遍历str1这个字符串,找到==0的

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();        HashMap
map=new HashMap<>(); //遍历str2这个字符串 for(int i=0;i

最长的数字串

题目:

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

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
='0'&&ch<='9'){ cur=cur+ch+""; }else{ if(cur.length()>ret.length()){ ret=cur; }else{ cur=""; } } } if(i==str.length()&&cur.length()>ret.length()){ ret=cur; } System.out.println(ret); }}

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

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>