遍历闹钟数组,小时转换为分钟,可以再优化,对数组排序。
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]); } }