搜狐笔试编程题题解
今天题目略简单,所以暴力也不会出现超时问题
1.系统设计
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); HashMap<Integer, Integer> hMap=new HashMap<>(); HashMap<Integer, Integer> hMap1=new HashMap<>(); for(int i=0;i<n;i++) { int flag=scanner.nextInt(); if(flag==1) { int start=scanner.nextInt(); int end=scanner.nextInt(); int value=scanner.nextInt(); for(int j=start;j<=end;j++) { if(hMap.containsKey(j)) hMap.put(j, Math.max(value, hMap.get(j))); else hMap.put(j, value); } } if(flag==2) { int day=scanner.nextInt(); int addvalue=scanner.nextInt(); if(hMap1.containsKey(day)) hMap1.put(day, Math.max(addvalue, hMap1.get(day))); else hMap1.put(day, addvalue); } } Object[] perDay=hMap.values().toArray(); Object[] work=hMap1.values().toArray(); int ans=0; for(int i=0;i<perDay.length;i++) ans+=(int)perDay[i]; for(int i=0;i<work.length;i++) ans+=(int)work[i]; System.out.println(ans); } }
2.查找k个最小值
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); int k=scanner.nextInt(); int[] res=new int[n]; for(int i=0;i<n;i++) { res[i]=scanner.nextInt(); } Arrays.sort(res); StringBuffer sBuffer=new StringBuffer(); for(int i=0;i<k-1;i++) sBuffer.append(res[i]+","); sBuffer.append(res[k-1]); System.out.println(sBuffer.toString()); } }
#实习#