牛客网2019真题-7-牛牛的闹钟

牛牛的闹钟

http://www.nowcoder.com/questionTerminal/9173e83d1774462f81255a26feafd7c6

遍历闹钟数组,小时转换为分钟,可以再优化,对数组排序。

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] hours = new int[n];
        int[] minute = new int[n];
        for(int i = 0; i < n; i++){
            hours[i] = sc.nextInt();
            minute[i] = sc.nextInt();
        }
        int time = sc.nextInt();
        int h = sc.nextInt();
        int m = sc.nextInt();

        int index = -1;
        int arrive = Integer.MAX_VALUE;
        for(int i = 0; i < n; i++){
            if(hours[i] <= h){
                int temp = (h - hours[i]) * 60 + m - minute[i];
                if(temp >= time && temp < arrive){
                    arrive = temp;
                    index = i;
                }
            }
        }
        System.out.println(hours[index] + " " + minute[index]);
    }


}
全部评论

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务