PAT1026:程序运行时间(Java实现)

tech2025-07-23  6

Scanner和BufferedReader性能相比简直了。

要注意的就是一点四舍五入,以及格式化输出。

package com.PAT; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class _1026 { public static void main(String[] args) throws IOException{ BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); String[] s = r.readLine().trim().split("\\s+"); int c1 = Integer.parseInt(s[0]); int c2 = Integer.parseInt(s[1]); r.close(); /* Scanner sc = new Scanner(System.in); int c1 = sc.nextInt(); int c2 = sc.nextInt();sc.close();*/ int CLK_TCK = 100; //不足 1 秒的时间四舍五入到秒 int time = Math. round((float)((c2-c1)*1.0)/CLK_TCK); // int time = (int)((c2 - c1) * 1.0 / CLK_TCK + 0.5); System.out.printf("%02d:%02d:%02d",time/3600,time/60%60,time%60); } }
最新回复(0)