C语言实验——时间间隔

来源:互联网 发布:天融信网络教育 编辑:程序博客网 时间:2024/06/10 21:21

C语言实验——时间间隔

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示。
如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时间先后顺序与输入无关。

Input

输入包括两行。
第一行为时间点1。
第二行为时间点2。

Output

以“小时:分钟:秒”的格式输出时间间隔。
格式参看输入输出。

Example Input

12:01:1213:09:43

Example Output

01:08:31

Hint

Author

import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);char s1[] = new char[20];char s2[] = new char[20];s1 = input.next().toCharArray();// 将字符串转换为字符数组s2 = input.next().toCharArray();// 将字符串转换为字符数组int a = (s1[0] - '0') * 10 + (s1[1] - '0');int b = (s1[3] - '0') * 10 + (s1[4] - '0');int c = (s1[6] - '0') * 10 + (s1[7] - '0');int d = (s2[0] - '0') * 10 + (s2[1] - '0');int e = (s2[3] - '0') * 10 + (s2[4] - '0');int f = (s2[6] - '0') * 10 + (s2[7] - '0');int t = a * 3600 + b * 60 + c - (d * 3600 + e * 60 + f);t = Math.abs(t);int x = t % 60;t = t / 60;int y = t % 60;t = t / 60;int z = t;System.out.println(String.format("%02d", z) + ":" + String.format("%02d", y) + ":" + String.format("%02d", x));//用String控制输出格式 input.close();}}



0 0
原创粉丝点击