首页 > 试题广场 >

牛牛的闹钟

[编程题]牛牛的闹钟
  • 热度指数:32198 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛总是睡过头,所以他定了很多闹钟,只有在闹钟响的时候他才会醒过来并且决定起不起床。从他起床算起他需要X分钟到达教室,上课时间为当天的A时B分,请问他最晚可以什么时间起床

输入描述:
每个输入包含一个测试用例。
每个测试用例的第一行包含一个正整数,表示闹钟的数量N(N<=100)。
接下来的N行每行包含两个整数,表示这个闹钟响起的时间为Hi(0<=A<24)时Mi(0<=B<60)分。
接下来的一行包含一个整数,表示从起床算起他需要X(0<=X<=100)分钟到达教室。
接下来的一行包含两个整数,表示上课时间为A(0<=A<24)时B(0<=B<60)分。
数据保证至少有一个闹钟可以让牛牛及时到达教室。


输出描述:
输出两个整数表示牛牛最晚起床时间。
示例1

输入

3 
5 0 
6 0 
7 0 
59 
6 59

输出

6 0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

class Node{
    int x=0;
    int y=0;
    Node(int x,int y){
        this.x=x;
        this.y=y;
    }
    Node(){}
}
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(br.readLine());
        ArrayList<Node> list=new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            String[] time=br.readLine().split(" ");
            Node node=new Node(Integer.parseInt(time[0]),Integer.parseInt(time[1]));
            list.add(node);
        }
        //起床到学校的时间
        int minutes=Integer.parseInt(br.readLine());

        String[] times=br.readLine().split(" ");
        int hour=Integer.parseInt(times[0]);
        int minute=Integer.parseInt(times[1]);
        //保存结果
        Node res=new Node();
        int min=Integer.MAX_VALUE;
        for(int i=0;i<n;i++)
        {
            Node node=list.get(i);

            if(hour<node.x||(hour==node.x&&minute<node.y)) continue;
            else if(hour>=node.x&&minute>=node.y)
            {
                int sum=(hour-node.x)*60+minute-node.y;
                if(sum>=minutes&&sum<=min) 
                {
                    res=node;
                    min=sum;
                }
            }
            else if(hour>=node.x&&minute<=node.y)
            {
                int sum=((hour-node.x-1)*60)+(60+minute-node.y);
                if(sum>=minutes&&sum<=min) 
                {
                    res=node;
                    min=sum;
                }
            }
        }
        System.out.println(String.format("%d %d",res.x,res.y));
    }
}

发表于 2022-08-15 17:26:39 回复(0)
思路:将小时先都化为分钟存到数组中,然后都加上到校时间X后,存到TreeSet中,调用floor()方法返回小于上课时间(也化为分钟)的最大值。
java代码
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        TreeSet<Integer> treeSet = new TreeSet<>();
        int[] arr = new int[N];
        for(int i=0;i<N;i++)
        {
            int Hi = sc.nextInt();
            int Mi = sc.nextInt();
            arr[i] = Hi*60+Mi;
        }
        int X = sc.nextInt();
        for(int i=0;i<N;i++)
        {
            treeSet.add(arr[i]+X);
        }
        int A = sc.nextInt();
        int B = sc.nextInt();
        sc.close();
        Integer res = treeSet.floor(A*60+B);
        System.out.println((res-X)/60+" "+(res-X)%60);
    }
}
发表于 2020-04-26 13:34:32 回复(0)
import java.util.Arrays;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //输入闹钟个数
        int n = sc.nextInt();
        int[] clock = new int[n];
        //输入闹钟时间
        for (int i = 0; i < n; i++) {
            int hour = sc.nextInt();
            int minute = sc.nextInt();
            clock[i] = hour*60 + minute;
        }
        Arrays.sort(clock);

        int walkTime = Integer.parseInt(sc.next());
        int hour = sc.nextInt();
        int minute = sc.nextInt();
        int classTime = hour*60+minute;
        int getupTime = classTime-walkTime;

        int i = n-1;
        //如果闹钟大于起床时间,继续往下找到最晚起床时间
        while (getupTime < clock[i]){
            i--;
        }
        System.out.println(clock[i]/60+" "+clock[i]%60);
    }
}

发表于 2020-04-12 12:56:40 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int[] arr=new int[N];
        for(int i=0;i<N;i++){
            int Hi=sc.nextInt();
            int Mi=sc.nextInt();
            arr[i]=Hi*60+Mi;
        }
        Arrays.sort(arr);
        int X=sc.nextInt();
        int A=sc.nextInt();
        int B=sc.nextInt();
        int sk=A*60+B;
        for(int i=0;i<N;i++){
            int t=sk-X-arr[i];
            if(t==0){
                System.out.print(arr[i]/60+" "+arr[i]%60);
                break;
            }
            if(t<0){
                System.out.print(arr[i-1]/60+" "+arr[i-1]%60);
                break;
            }
        }
    }
}

发表于 2020-02-25 11:08:55 回复(1)
//时转换为分并存储,找出满足条件最大的即可
发表于 2020-02-20 13:16:08 回复(0)
import java.util.Scanner;

public class Main {
            public static void main(String[] args) {
                 Scanner in=new Scanner(System.in);
                    int n=in.nextInt();
                    int a[]=new int[n];
                    int h=0,m=0;
                    for(int i=0;i<n;i++){
                        h=in.nextInt();
                        m=in.nextInt();
                        a[i]=60*h+m;
                    }
                    int min = in.nextInt();
                    h=in.nextInt();
                    m=in.nextInt();
                    int time = 60*h+m;
                    int max=0;
                    for(int i=0;i<n;i++){
                        if(a[i]+min<=time){
                            max = Math.max(a[i], max);
                        }
                    }
                    System.out.println(max/60+" "+max%60);
                    
            }
发表于 2019-09-11 22:54:03 回复(0)
import java.util.*;
//1.将输入的闹钟时间转换成分钟数存入矩阵,方便计算
//2.将上课时间减去到达教室所需时间,得到起床时间
//3.由于只有闹钟响时才决定起不起床,因此在小于等于起床时间的所有闹钟里面,最大的那个就是我们需要的最晚闹钟时间
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        //时间转换成分钟数
        int[] minutes = new int[N];
        int hour = 0;
        int minute = 0;
        for(int i = 0; i < N; i++){
            hour = sc.nextInt();
            minute = sc.nextInt();
            minutes[i] = hour * 60 + minute;
        }
        int needTime = sc.nextInt();
        hour = sc.nextInt();
        minute = sc.nextInt();
        int classTime = hour * 60 + minute;
        //得到最晚起床时间
        int lastMinute = classTime - needTime;
        //设置一个max,在小于等于起床时间的闹钟时间里,更新最大的闹钟时间为max
        int max = 0;
        for(int i = 0; i < N; i++){
            if(lastMinute >= minutes[i]){
                max = Math.max(max, minutes[i]);
            }
        }
        //转换成时间
        hour = max / 60;
        minute = max % 60;
        System.out.print(hour + " " + minute);
    }
}

发表于 2019-09-03 10:15:51 回复(0)
import java.util.Scanner;
import java.util.ArrayList;
public class Main{
    public static void main(String [] args)
    {
        /*
        解题思路:
        1.接受读取数据
        2.排序(是否默认已经排好序)
        3.遍历
        */
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int [] hour = new int[N];    //时
        int [] min = new int[N];     //分
        ArrayList alarms =  new ArrayList<Alarm>();
        for(int i=0; i<N; i++)        //读取闹钟信息
        {
        //    hour[i] = sc.nextInt();
         //   min[i] = sc.nextInt();        
            int h = sc.nextInt();
            int m = sc.nextInt();
            Alarm a = new Alarm(h, m);
            alarms.add(a);
        }
        
       int needTime = sc.nextInt();        //获取到所需要的时间
        int hourTime = sc.nextInt();        //上课时
        int minTime = sc.nextInt();        //上课分
        // que(arr, 0, N-1);    //排序
        que(alarms, 0, N-1);
        
        for(int i=0; i<N; i++)
        {
            Alarm a1 = (Alarm)alarms.get(N-i-1);
               if(InTime(a1.getHour(), a1.getMin(), needTime, hourTime, minTime))
            {
                
                System.out.println(a1.getHour() + " " + a1.getMin());
                break;
            }
       /*     if(InTime(hour[N-i-1], min[N-i-1], needTime, hourTime, minTime))
            {
                
                  System.out.println(hour[N-i-1] + " " + min[N-i-1]);
                break;
            }
         */     
            
        }
    }
    
    public static void  que(ArrayList <Alarm> arr, int low, int high)
    {
        if(low < high)
        {
           int mid =  par(arr, low, high);
            que(arr, low, mid-1);
             que(arr, mid+1, high);
            
        }
    }
    //比较两个alarm, a1是否大于a2
    public static boolean com(Alarm a1, Alarm a2)
    {
        if(a1.getHour() < a2.getHour())
            return false;
        
        if(a1.getHour() == a2.getHour() && a1.getMin() < a2.getMin())
            return false;
        return true;
   /*     
        if(a1.getHour() >= a2.getHour())
            return true;
        else
        {
            if(a1.getHour() == a2.getHour() && a1.getMin() > a2.getMin())
            return true;
    
        return false;    
        }*/
        
         /*   if(a1.getHour() < a2.getHour())
            return false;
        if(a1.getMin() >= a2.getMin())
            return true;
       */
    }
    //Alarm排序, 用快排
    public static int par(ArrayList <Alarm> arr, int low, int high)
    {
        
     //找到low的正确位置
        Alarm a = arr.get(low);        //取出low
            int hour = a.getHour();
            int min = a.getMin();
            Alarm temp = new Alarm(hour, min);
        while(low < high)
        {
            
            while(com(arr.get(high), temp) && low<high)        //a右边的比a大的出现那么
            {
                high--;
            }
            Alarm a1 = arr.get(low);        //取出low
            a1.setHour(arr.get(high).getHour());
            a1.setMin(arr.get(high).getMin());
            
            while(com(temp, arr.get(low)) && low<high)
            {
                low++;
            }
            
             Alarm a2 = arr.get(high);        //取出low
            a2.setHour(arr.get(low).getHour());
            a2.setMin(arr.get(low).getMin());
        }
            
            Alarm a3 = arr.get(low);        //取出low
            a3.setHour(hour);
            a3.setMin(min);
            
          
            return low;
        
    }
    
    
    //这是一个比较该闹钟是否能准时赶到的函数
   public static boolean InTime(int hour, int min, int needTime, int hourTime, int minTime)
    {
        if(hour >hourTime)        //如果闹钟的时钟超过hourTime则不可能了
            return false;
        
        int secHour = hourTime-hour;    //计算得到小时的差值
        int secMin = secHour*60 + minTime - min;    //把所有的多余时间转换为分钟
        
        
        return secMin >= needTime?true:false;
    }
    
}

class Alarm
{
    private int hour;    //时
    private int min;     //分
    public Alarm()
    {
        
    }
    
    public Alarm(int h , int m)
    {
        this.hour = h;
        this.min = m;
    }
    public void setHour(int hour)
    {
        this.hour = hour;
    }
    public void setMin(int min)
    {
        this.min = min;
    }
    
    public int getHour()
    {
        return this.hour;
    }
    public int getMin()
    {
        return this.min;
    }
        
}
发表于 2019-08-28 19:20:12 回复(0)
1.将所有闹钟时间转换成分钟形式,存入集合中
2.遍历集合,判断该事件是否满足不迟到,如果满足,再判断是否为为最晚闹钟,如果是,标记该闹钟。
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			ArrayList<Integer> array = new ArrayList<Integer>();
			for(int i = 0;i<n;i++) {
				int p = sc.nextInt();
				int q = sc.nextInt();
				array.add(p*60+q);
			}
			int need = sc.nextInt();
			int hour = sc.nextInt();
			int second = sc.nextInt();
			int overTime = hour*60 + second;
			int num = 0;
			int abs = overTime;
			for(int i =0;i<n;i++) {
				if((array.get(i)+need)<=overTime) {
						if(overTime-(array.get(i)+need)<=abs) {
							abs = overTime-(array.get(i)+need);
							num = i;
						}	
				}
			}		
			System.out.println(array.get(num)/60 + " "+array.get(num)%60);
		}
	}
}


编辑于 2019-08-26 16:49:07 回复(1)

import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[][] array =new int [N][2];
for (int i = 0; i <N; i++)
{
array[i][0]=sc.nextInt();
array[i][1]=sc.nextInt();

    }
    int x=sc.nextInt();
    int A=sc.nextInt();
    int B=sc.nextInt();
    A=(int) ((A+Math.floor((B-x)/60.0)+24)%24);
    B=(B-x+120)%60;
    int hour=0;
    int minute=0;
    for (int i = 0; i < N; i++)
    if(array[i][0]<A||array[i][0]==A&&array[i][1]<=B)
    {
        if(hour<array[i][0])
        {
            hour=array[i][0];
            minute=array[i][1];
        }
        else if (hour==array[i][0]&&minute<array[i][1]) {
            hour=array[i][0];
            minute=array[i][1];
        }
    }
        System.out.println(hour+" "+minute);
    }            

}

发表于 2019-06-30 16:27:59 回复(1)
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    static class Time {
        private int hour;
        private int min;
        public Time(int hour, int min) {
            this.hour = hour;
            this.min = min;
        }
    }

    public static class TimeComparator implements Comparator<Time> {
        public int compare(Time o1, Time o2) {
            return o1.hour != o2.hour ?  o2.hour - o1.hour : o2.min - o1.min;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Time[] time = new Time[n];
        for (int i = 0; i < n; i++) {
            time[i] = new Time(sc.nextInt(), sc.nextInt());
        }
        int need = sc.nextInt();
        Time classTime = new Time(sc.nextInt(), sc.nextInt());

        while (need > 0) {
            if (need >= 60) {
                need -= 60;
                classTime.hour--;
            } else {
                if (classTime.min - need < 0) {
                    classTime.min = 60 - (need - classTime.min);
                    classTime.hour--;
                } else {
                    classTime.min -= need;
                }
                break;
            }
        }
        Arrays.sort(time, new TimeComparator());
        for (int i = 0; i < n; i++) {
            if (classTime.hour > time[i].hour || (classTime.hour == time[i].hour && classTime.min >= time[i].min)) {
                System.out.println(time[i].hour + " " + time[i].min);
                break;
            }
        }

    }
}
发表于 2019-06-30 10:45:05 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         List<Integer> min = new ArrayList<>();         int N = sc.nextInt();         for(int i=0;i<N;i++){             int H = sc.nextInt();             int M = sc.nextInt();             min.add(H*60+M);         }                  int X = sc.nextInt();         int time = sc.nextInt()*60+sc.nextInt();                  lastClock(min,X,time);
     }          public static void lastClock(List<Integer> clock,int X,int time){                  int last = time - X;         clock.sort(new Comparator<Object>(){             @Override             public int compare(Object o1, Object o2) {                 // TODO 自动生成的方法存根                 return (Integer)o1 - (Integer)o2;             }         });                  for(int i=1;i<clock.size();i++){             if(clock.get(i)>last){                 System.out.println(clock.get(i-1)/60 + " "+clock.get(i-1)%60);                 break;
            }         }              }
}
发表于 2018-04-02 11:48:17 回复(0)

思路

把时间都转换为分钟计数,上课时间-路上时间得到最晚起床时间,把所有闹钟时间排序后,二分查找最晚起床时间。

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int h=0,m=0;
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            h = sc.nextInt();
            m = sc.nextInt();
            a[i] = h*60+m;
        }
        int t = sc.nextInt();
        h = sc.nextInt();
        m = sc.nextInt();
        int p = h*60+m-t;
        Arrays.sort(a);
        t = Arrays.binarySearch(a,p);
        if(t <0) t = -(t+2);
        h = a[t]/60;
        m = a[t]%60;
        System.out.print(h+" "+m);
    }
}
发表于 2018-03-28 11:17:54 回复(12)

热门推荐

通过挑战的用户

查看代码