首页 > 试题广场 >

小乐乐定闹钟

[编程题]小乐乐定闹钟
  • 热度指数:65934 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

小乐乐比较懒惰,他现在想睡觉,然后再去学习。他知道现在的时刻,以及自己要睡的时长,想设定一个闹钟叫他起床学习,但是他太笨了,不知道应该把闹钟设定在哪个时刻,请你帮助他。(只考虑时和分,不考虑日期)


输入描述:

输入现在的时刻以及要睡的时长k(单位:minute),中间用空格分开。

输入格式:hour:minute k(如hour或minute的值为1,输入为1,而不是01)

(0 ≤ hour ≤ 23,0 ≤ minute ≤ 59,1 ≤ k ≤ 109)



输出描述:
对于每组输入,输出闹钟应该设定的时刻,输出格式为标准时刻表示法(即时和分都是由两位表示,位数不够用前导0补齐)。
示例1

输入

0:0 100

输出

01:40
示例2

输入

1:0 200

输出

04:20
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strs = in.nextLine().split(" ");
        String[] times = strs[0].split(":");
        int hour = Integer.parseInt(times[0]);
        int minute = Integer.parseInt(times[1]);
        int currentTimeToMinute = hour*60 + minute;
        int clockTimeInMinute=currentTimeToMinute+Integer.parseInt(strs[1]);
        int newHour = (clockTimeInMinute/60)%24;
        int newMinute = clockTimeInMinute%60;
        System.out.printf("%s:%s",newHour<10?"0"+newHour:newHour,newMinute<10?"0"+newMinute:newMinute);
    }
}
发表于 2024-05-08 10:25:18 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s=in.nextLine();
String[] sList=s.split(":| ");
int a=Integer.valueOf(sList[0]);
int b=Integer.valueOf(sList[1]);
int c=Integer.valueOf(sList[2]);

int h=a+c/60+(b+c%60)/60;
int m=b+c%60;
System.out.printf("%02d:%02d",h%24,m%60);

}
}
发表于 2022-10-26 10:40:38 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] toTimeStr = scanner.nextLine().split(":| ");
        int curHour = Integer.parseInt(toTimeStr[0]);
        int curMinute = Integer.parseInt(toTimeStr[1]);
        int toSleepDuration = Integer.parseInt(toTimeStr[2]);
        curMinute = curMinute + toSleepDuration;
        curHour = curHour + (curMinute / 60);
        curMinute = curMinute % 60;
        curHour = curHour % 24;
        System.out.println(String.format("%02d",curHour) + ":" + String.format("%02d",curMinute));     
    }
}

发表于 2022-06-24 09:29:44 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String time = sc.nextLine();
        int index1 = time.indexOf(":");
        int index2 = time.indexOf(" ");
        int h = Integer.parseInt(time.substring(0, index1));
        int m = Integer.parseInt(time.substring(1 + index1, index2));
        int k = Integer.parseInt(time.substring(index2 + 1));
        int l = h * 60 + m + k;
        if (l >= 1440) {
            l = l % 1440;
        }
        int h1 = l / 60;
        int m1 = l % 60;

        if (h1 < 10) {
            System.out.print("0" + h1 + ":");
        } else {
            System.out.print(h1 + ":");
        }
        if (m1 < 10) {
            System.out.print("0" + m1);
        } else {
            System.out.print(m1);
        }
    }
}

发表于 2022-06-15 11:40:36 回复(0)
import java.util.Scanner;

/**
 *
 * @Title          小乐乐定闹钟
 * @Description    <TODO>
 * 小乐乐比较懒惰,他现在想睡觉,然后再去学习。
 * 他知道现在的时刻,以及自己要睡的时长,想设定一个闹钟叫他起床学习,但是他太笨了,
 * 不知道应该把闹钟设定在哪个时刻,请你帮助他。(只考虑时和分,不考虑日期)
 *
 * 输入描述:
 *  输入现在的时刻以及要睡的时长k(单位:minute),中间用空格分开。
 *
 * 输入格式:hour:minute k(如hour或minute的值为1,输入为1,而不是01)
 *
 * (0 ≤ hour ≤ 23,0 ≤ minute ≤ 59,1 ≤ k ≤ 109)
 *
 * 输出描述:
 *  对于每组输入,输出闹钟应该设定的时刻,输出格式为标准时刻表示法(即时和分都是由两位表示,位数不够用前导0补齐)。
 *
 * @author weijunfu<ijunfu @ qq.com>
 * @date 2022/03/15 11:41
 * @version 1.0.0
 *
 */

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String[] s = in.nextLine().split(" ");

        String[] times = s[0].split(":");

        int hour = Integer.parseInt(times[0]);
        int minute = Integer.parseInt(times[1]);

        int minutes = Integer.parseInt(s[1]);

        minute = minute + minutes;

        hour += minute/60;

        minute = minute%60;

        System.out.printf("%02d:%02d", hour%24, minute);

        in.close();
    }
}
注意:一天按24小时计算

发表于 2022-03-15 12:51:49 回复(0)
//难点:split和模除法进位
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String[] strs = scan.nextLine().split(":| ");
        int curHour = Integer.parseInt(strs[0]);
        int curMinute = Integer.parseInt(strs[1]);
        int minute = Integer.parseInt(strs[2]);
        curMinute += minute;
        curHour += (curMinute / 60);
        curMinute %= 60;
        curHour %= 24;
        System.out.printf("%02d:%02d", curHour, curMinute);
    }
} 

发表于 2021-10-24 23:07:07 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            String []clock=sc.nextLine().split("\\:| ");
            int hour =Integer.parseInt(clock[0]);
            int minute =Integer.parseInt(clock[1]);
            int k=Integer.parseInt(clock[2]);
            while(k>60){
                hour++;
                if(hour>=24){
                    hour=0;
                }
                k-=60;
            }
            int l=k+minute;
            if(l>60){
                hour++;
                l-=60;
            }
            System.out.println(String.format("%02d:%02d",hour,l));
        }
    }
}

发表于 2021-10-21 18:30:33 回复(0)
import java.util.*;

public class Main{
    public static void main(String [] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str1[] = sc.next().split(":");
            int  relaxTime = sc.nextInt();
            int nowtime[] = new int[10];
            int j = 0;
            for(String i : str1){

                nowtime[j++] = Integer.parseInt(i);
            }
            int hour = relaxTime/60;
            int minute = relaxTime%60;
            nowtime[0] = nowtime[0]+hour;
            nowtime[1] = nowtime[1]+minute;
            if(nowtime[1]>=60){
                nowtime[0] = nowtime[0]+nowtime[1]/60;
                nowtime[1] = nowtime[1]%60;
            }
            if(nowtime[0]>=24)
            {
                nowtime[0] = nowtime[0]%24;
            }
            StringBuilder sb = new StringBuilder();
            if(nowtime[0]<10){
                sb.append("0"+nowtime[0]+":");
            }
            else {
                sb.append(""+nowtime[0]+":");
            }
            if(nowtime[1]<10){
                sb.append("0"+nowtime[1]);
            }
            else {
                sb.append(""+nowtime[1]);
            }
            System.out.println(sb);
        }
    }
}

发表于 2021-10-06 16:30:03 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int time = Integer.parseInt(str[1]);
        String[] hourArray = str[0].split(":");
        int hour = Integer.parseInt(hourArray[0]);
        int minute = Integer.parseInt(hourArray[1]);
        int t = time / 60;
        int e = time % 60;
        int newMinute = minute + e;
        if (newMinute >= 60) {
            newMinute %= 60;
            t += 1;
        }
        int newHour = hour + t;
        if (newHour >= 24) {
            newHour %= 24;
        }

        System.out.printf("%02d:%02d\n", newHour, newMinute);
    }
}

发表于 2021-07-16 20:37:02 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            String[] message = scanner.nextLine().split(" ");
            String[] hourAndMinute = message[0].split(":");
            int hour = Integer.parseInt(hourAndMinute[0]);
            int minute = Integer.parseInt(hourAndMinute[1]);
            long clock = Long.parseLong(message[1]);
            long newMinute = (clock % 60 + minute)%60;
            long newHour = (clock / 60 + hour + (clock % 60 + minute)/60)%24;
            System.out.printf("%02d:%02d", newHour, newMinute);
        }
    }
}//清楚关系式即可
发表于 2021-03-19 21:39:33 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String[] s = sc.nextLine().split(" ");
            String[] time = s[0].split(":");
            int sleep = Integer.parseInt(s[1]);
            int hour = sleep/60;
            int minute = sleep%60;
            int curH = Integer.parseInt(time[0]);
            int curM = Integer.parseInt(time[1]);
            curH = (curH+hour+(minute+curM)/60)%24;
            curM = (curM+minute)%60;
            System.out.println(String.format("%02d",curH)+":"+ String.format("%02d",curM));
        }
    }
}


发表于 2020-10-07 16:20:03 回复(0)