搜狐笔试编程题题解

今天题目略简单,所以暴力也不会出现超时问题
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());

    }

}

#实习#
全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
点赞 8 评论
分享
牛客网
牛客企业服务