华为9.20笔试,591分
第一题
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] S = new int[n]; HashMap<Integer,List<Integer>> map = new HashMap<>(); for(int i=0;i<n;i++) { S[i] = in.nextInt(); map.computeIfAbsent(S[i],k->new ArrayList<>()); map.get(S[i]).add(i); } int t = in.nextInt(); List<Integer> list = map.get(t); int begin = -1; int end = -1; if(list==null){ System.out.print(begin+" "+end); return; } int last = list.get(0); for(int i=1;i<list.size();i++){ int num = list.get(i); if(num!=last+1){ begin = num; end = last; System.out.print(begin+" "+end); return; } last = num; } begin = list.get(0); end = list.get(list.size()-1); System.out.print(begin+" "+end); } }
第二题bfs,代码丢了
第三题 通过用例97%,剩余用例cpu错误
import java.util.*; public class Main { static HashMap<String,Long> vars = new HashMap<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNextLine()){ String line = in.nextLine(); if(line.charAt(0)=='l'){ handleDefine(line); } if(line.charAt(0)=='o'){ handleOut(line); } } } static int mark; static int state; public static void handleOut(String line){ mark = 4; StringBuilder varNameBuilder = new StringBuilder(); if(!checkVarName(line.charAt(mark))||('0'<=line.charAt(mark)&&line.charAt(mark)<='9')){ System.out.println("<syntax-error>"); return; } while(mark<line.length()&&checkVarName(line.charAt(mark))){ varNameBuilder.append(line.charAt(mark)); mark++; } String varName = varNameBuilder.toString(); if(vars.get(varName)==null){ System.out.println("<undefined>"); }else if(vars.get(varName)>Integer.MAX_VALUE){ System.out.println("<overflow>"); }else if(vars.get(varName)<Integer.MIN_VALUE){ System.out.println("<underflow>"); }else{ System.out.println(vars.get(varName)); } } public static void handleDefine(String line){ mark = 4; state = 0; StringBuilder varNameBuilder = new StringBuilder(); if(!checkVarName(line.charAt(mark))||('0'<=line.charAt(mark)&&line.charAt(mark)<='9')){ System.out.println("<syntax-error>"); return; } while(mark<line.length()&&checkVarName(line.charAt(mark))){ varNameBuilder.append(line.charAt(mark)); mark++; } if(mark<line.length()&&(line.charAt(mark)!=' '&&line.charAt(mark)!='=')){ System.out.println("<syntax-error>"); return; } String varName = varNameBuilder.toString(); skipSpace(line); mark++; skipSpace(line); long res = handleCount(line); if(state==0) { vars.put(varName, res); } } public static long handleCount(String line){ long res = getVarOrInteger(line); if(state==-1) return 0; while (mark<line.length()){ skipSpace(line); char c = line.charAt(mark); switch (c){ case '+':{ mark++; skipSpace(line); res += getVarOrInteger(line); if(state==-1) return 0; break; } case '-':{ mark++; skipSpace(line); res -= getVarOrInteger(line); break; } case '*':{ mark++; skipSpace(line); res *= getVarOrInteger(line); break; } case '/':{ mark++; skipSpace(line); res /= getVarOrInteger(line); break; } } skipSpace(line); } return res; } public static long getVarOrInteger(String line){ StringBuilder varBuilder = new StringBuilder(); boolean beginByInt =false; if(!checkVarName(line.charAt(mark))){ if(line.charAt(mark)!='-'){ System.out.println("<syntax-error>"); state = -1; return 0; } } if('0'<=line.charAt(mark)&&line.charAt(mark)<='9'){ beginByInt = true; } while(mark<line.length()&&checkVarName(line.charAt(mark))){ varBuilder.append(line.charAt(mark)); mark++; } if(varBuilder.length()==0) return 0; long res = 0; String var = varBuilder.toString(); try{ res = Long.parseLong(var); }catch (Exception e){ if(beginByInt){ System.out.println("<syntax-error>"); state = -1; return 0; } if(vars.get(var)!=null){ res = vars.get(var); }else{ state = -1; return res; } } return res; } public static boolean checkVarName(char c){ if(('a'<=c&&c<='z')||'A'<=c&&c<='Z'){ return true; } if('0'<=c&&c<='9') return true; return c == '_'; } public static void skipSpace(String line){ while(mark<line.length()&&line.charAt(mark)==' ') mark++; } }#华为求职进展汇总##如何看待2023届秋招##我的实习求职记录#