蓝桥杯真题-Java-时间显示-第十二届-研究生组-省赛
时间显示
内存限制: 256.0MB; C/C++时间限制:1.0s; Java时间限制:3.0s ; Python时间限制:5.0s
题目分析:
方法1:不使用Java中自带的的API完成
因为题目不要求显示年月日,只需要显示出时分秒即可,因此以我们不需要考虑1970年1月1日这个条件。再加之时分秒也是从00:00:00开始的,所以仅需通过整除取余获取所输入数字所对应的时分秒,再按照标准的输出格式完成输出即可。
a. 1 毫秒=0.001 秒
b. 小时 = time /1000 / 60 / 60 % 24;
c. 分钟 = time / 1000 / 60 % 60;
d. 秒 = time / 1000 % 60。
所以可得到如下代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long N = scanner.nextLong();
long hours = N/1000/60/60%24;
long minutes = N/1000/60%60;
long seconds = N/1000%60;
if(hours < 10) {
System.out.print("0" + hours + ":");
}else{
System.out.print(hours + ":");
}
if(minutes < 10) {
System.out.print("0" + minutes + ":");
}else {
System.out.print(minutes + ":");
}
if(seconds < 10) {
System.out.print("0" + seconds);
}else {
System.out.print(seconds);
}
}
}
运行结果如下:
方法2:使用Java中自带的的API完成
此部分将于后续对此接口了解更多后更新。