基本都被卡64%通过率。。。
题目一:输入nMax个数的数组, 找恰好有num个数大于的一个阈值。可能有重复数字
import java.util.Arrays; import java.util.Scanner; public class meituan1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for(int i = 0;i < n;i ++){ int nMax = scanner.nextInt(); int num = scanner.nextInt(); int[] ints = new int[nMax]; for(int j = 0 ;j < nMax;j ++){ ints[j] = scanner.nextInt(); } int i1 = find(ints, nMax, num); if(i1 == -1){ System.out.println("NO"); }else{ System.out.println("YES"); System.out.println(i1); } } } private static int find(int[] ints, int nMax, int num) { int res = -1; if(ints.length < 0){ return res; }else{ Arrays.sort(ints); int count = 1; for(int i = 1;i < ints.length; i++){ count ++; if (count == nMax ){ break; } if(count == num ){ if(i+1 < ints.length && ints[i + 1] != ints[i]){ res = ints[i]+1; }else if(i == ints.length - 1){ res = ints[i]+1; } } } } return res; } }题目二:清洗数组,清洗掉空格和紧挨着的重复字符
package com.mianshi; import java.util.Scanner; public class meituan2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String next = scanner.nextLine(); String replace = next.replace(" ", ""); System.out.println(replace); if(replace.length() == 1){ System.out.println(replace); }else { for (int i = 1; i < replace.length();) { if (replace.charAt(i) == replace.charAt(i - 1)){ replace = replace.substring(0,i) + replace.substring(i+1,replace.length()); }else{ i ++ ; } } System.out.println(replace); } } }题目三:输入一个数组ints,根据这个数组生成一个新的数组dp,i位置的数值是不大于ints[i]的前1~i的最大的那个数,求新数组的dp[i]*i 之和,i=1 ~n
package com.mianshi; import java.util.Scanner; public class meituan3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] ints = new int[n + 1]; int[] dp = new int[n+1]; long res = 0; for(int i = 1;i <= n;i ++){ ints[i] = scanner.nextInt(); } for(int j = 1;j <= n; j++){ for(int k = 0 ;k < j;k++){ if(ints[k] < ints[j]){ dp[j] = Math.max(dp[j],ints[k]);} } res += dp[j]*j; } System.out.println(res); } }题目四:将一个数组左半和右半变成一样的操作数。
package com.mianshi; import java.util.Scanner; public class meituan4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] ints1 = new int[n/2]; int[] ints2 = new int[n/2]; int res = 0; for(int i = 0;i < n;i ++){ if(i < n/2){ ints1[i] = scanner.nextInt(); }else{ ints2[i - n/2] = scanner.nextInt(); } } for(int i = 0;i < n/2;i ++){ if (ints1[i] != ints2[i]){ res ++; } } System.out.println(res); } }题目五:设计一个测试用例,测试用户密码的登陆功能。
全部评论
(2) 回帖