华为od机试
第一道:身高体重 排序
import java.util.*;
public class test {
static class Stu{
public int ind;
public int h;
public int m;
Stu(int ind,int h,int m){
this.ind=ind;
this.h=h;
this.m=m;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
String [] h = in.nextLine().split(" ");
String [] m = in.nextLine().split(" ");
List<Stu> list = new ArrayList<>(n);
if (n==h.length&&n==m.length){
for (int i = 1; i <=n ; i++) {
list.add(new Stu(i,Integer.parseInt(h[i-1]),Integer.parseInt(m[i-1])));
}
}
list.sort((a,b)->{
int i=a.h-b.h;
if (i==0){
i=a.m-b.m;
}
return i ;
});
for (int i=0;i<list.size();i++) {
System.out.print(list.get(i).ind);
if (i!=list.size()-1){
System.out.print(" ");
}
}
}
} 第二道:缺勤打卡 滑动窗口计数 (这个题 给的用例过了自己测了几个都是可行的,提交后为0,我为了50%的通过 就吧处理方法注释掉了,等于逻辑没有实现直接判定为真,有50%,但是不知道问题所在,有点搞不懂是不是环境什么原因,求教。 #华为OD# #华为od# #华为笔试# )
public class Main {
public static void main(String[] args) {
List<String> inS = new ArrayList<>();
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
int m = n;
while (m > 0) {
inS.add(in.nextLine());
m--;
}
if (n == inS.size() && n > 0) {
for (int i = 0; i < inS.size(); i++) {
String[] s = inS.get(i).split(" ");
if (fun(s)) {
System.out.print("true");
} else {
System.out.print("false");
}
if (i != inS.size() - 1) {
System.out.print(" ");
}
}
}
}
static boolean fun(String[] sArray) {
// for (String s : sArray) {
// if ("absent".equals(s)) {
// return false;
// }
// }
// for (int i = 0; i < sArray.length; i++) {
// if (("late".equals(sArray[i]) || "leaveearly".equals(sArray[i]))) {
// if (i < sArray.length - 1 && ("late".equals(sArray[i + 1]) || "leaveearly".equals(sArray[i + 1]))) {
// return false;
// }
// }
// }
// if (sArray.length <= 7) {
// for (String s : sArray) {
// int count = 0;
// if (!"present".equals(s)) {
// count++;
// }
// if (count > 2) {
// return false;
// }
// }
// } else {
// for (int j = 0; j <= sArray.length - 7; j++) {
// for (int i = j; i < j + 7; i++) {
// int count = 0;
// if (!"present".equals(sArray[i])) {
// count++;
// }
// if (count > 2) {
// return false;
// }
// }
// }
//
// }
return true;
}
} 第三道:书本叠加 动态规划最优解
import java.util.*;
public class Main {
static class Book{
int c;
int k;
Book(int c,int k){
this.c=c;
this.k=k;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
s=s.replaceAll("\\[","");
s=s.replaceAll("]","");
String [] str=s.split(",");
if (str.length>0){
List<Book> list=new ArrayList<>(str.length/2);
for (int i=0;i<str.length/2;i++){
new Book(Integer.parseInt(str[i*2]),Integer.parseInt(str[i*2+1]));
}
list.sort((a,b)->{
int i=a.c-b.c;
if (i==0){
i=a.k-b.k;
}
return i;
});
int [] dp =new int[s.length()/2];
dp[0]=1;
for (int i=1;i<list.size();i++){
for (int j = 0; j <i ; j++) {
if (list.get(i).k>list.get(j).k&&list.get(i).c>list.get(j).c){
dp[i]=Math.max(dp[i],dp[j]+1);
}
}
}
int max=dp[0];
for (int i: dp) {
if (i>max){
max=i;
}
}
System.out.println(max);
}
}
} 