蓝桥杯——时间显示

眼看着就要蓝桥杯了,但自己会做的算法题也就那么几个,在慌里慌张中选择了做蓝桥杯真题来练手感,不做不要紧,一做真题,仅存的那点自信心也没有了。并且还深刻意思到算法这条路还有很长的路要走,不知何时才能再上一个水平,可能时间真的有些不多了。在蓝桥杯里面翻来覆去去找题做,几乎没几个会写的,甚至有的题看了答案都不理解为啥可以这样写,知道现在都不理解。算了,不说了,就记录一下这几天写的较容易的一道题吧。
时间显示这道题,个人感觉跟求一个数字的各个位是多少的题没啥区别,唯一一点就是它的输出格式
可以是System.out.printf("%02d:%02d:%02d",hour,minute,second);。也可以是System.out.println(String.format("%02d:%02d:%02d",hour,minute,second));。还有一种就是直接使用java的API
写出来,先new 一个Date对象,参数传毫秒-836001000,因为java的Date是从1970年1月1日08:00开始算的,本题是从00:00开始算的,然后就是new一个简单日期格式类传一个要显示形式的字符串,然后用这个类的对象去显示刚new出来的日期类对象就行了。
代码:

import java.util.Scanner;
public class 时间显示 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		long[] arr=new long[n];
		for(int i=0;i<n;i++) {
			arr[i]=sc.nextLong();
			long h=0;
			long m=0;
			long s=0;
			h=arr[i]/3600000%24;
			m=arr[i]/60000%60;
			s=arr[i]/1000%60;

			System.out.println(String.format("%02d:%02d:%02d", h,m,s));
			sc.close();
		}	
	}
}

另一种:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class test {

	public static void main(String[] args) {
		 Scanner sc=new Scanner(System.in);
		 int n=sc.nextInt();
		 long[]arr=new long[n];
		 for(int i=0;i<n;i++) {
			 arr[i]=sc.nextLong();
			 Date date=new Date(arr[i]-8*1000*3600);
			 SimpleDateFormat sf=new SimpleDateFormat("HH:mm:ss");
			 System.out.println(sf.format(date));
		 }
	}

}