华为OD机试(2022/04/24)
第一题
因式分解:求素数乘积
输入一个数,求该数的素数乘积
输入:21
输出:3 7
第二题
括号的最大嵌套深度
输入一个仅由 "()[]{}" 组成的字符串,计算出有效的括号最大嵌套深度
输入:([]{()})
输出:3
第三题
第一行输入一个非负整数字符串,第二行输入一个整数n
输出从该字符串中取出n个字符后剩下的字符组成的最小的数(不改变字符顺序)
输入 :
2615371
4
输出:131
我的代码
不可否认,一直在凑用例通过率,最后400分通过
第一题
public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = sc.nextInt(); long k = (long)Math.sqrt(n); int a = -1; int b = -1; for(int i=2;i<=k;i++){ if(n%i==0 && isPrime(i) && isPrime(n/i)){ a = i; b = n/i; break; } } System.out.println(a+" "+b); } private static boolean isPrime(int x){ if(x<=1) return false; for(int i=2;i<=(long)Math.sqrt(x);i++){ if(x%i==0){ return false; } } return true; }
第二题
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int res = 0; int temp = 0; Stack<Character> stack = new Stack<>(); for(int i=0;i<s.length();i++){ Character c = s.charAt(i); if(c=='(') { stack.push(')'); temp++; res = Math.max(res,temp); }else if(c=='[') { stack.push(']'); temp++; res = Math.max(res,temp); }else if (c=='{') { stack.push('}'); temp++; res = Math.max(res,temp); }else { if((c==')'||c==']'||c=='}')&&stack.size()>0){ Character pop = stack.pop(); if (pop==c){ temp--; } } } } if(temp!=0)res = 0; System.out.println(res); }
第三题
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.nextLine(); int n = sc.nextInt(); StringBuilder num1 = new StringBuilder(num); int start = 0; int m = num1.length()-n; StringBuilder sb = new StringBuilder(); for(int i=1;i<=m;i++){ if(num1.substring(start,n+i).contains("0")){ sb.append(0); start = num1.indexOf("0")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } }else if(num1.substring(start,n+i).contains("1")){ sb.append(1); start = num1.indexOf("1")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } }else if(num1.substring(start,n+i).contains("2")){ sb.append(2); start = num1.indexOf("2")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("3")){ sb.append(3); start = num1.indexOf("3")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("4")){ sb.append(4); start = num1.indexOf("4")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("5")){ sb.append(5); start = num1.indexOf("5")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("6")){ sb.append(6); start = num1.indexOf("6")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("7")){ sb.append(7); start = num1.indexOf("7")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("8")){ sb.append(8); start = num1.indexOf("8")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } else if(num1.substring(start,n+i).contains("9")){ sb.append(9); start = num1.indexOf("9")+1; for(int j=1;j<=start;j++){ num1 = num1.replace(j-1,j,"h"); } } } if(sb.length()<=0){ System.out.println(0); }else{ System.out.println(Long.valueOf(sb.toString())); } }#华为笔试##笔试题目##华为#