有一时钟在某时刻的时间为HH:MM:SS(24小时制),求时钟在N秒(N<86400)之前的时间hh:mm:ss。 输入: 第一行输入时间HH:MM:SS 第二行输入秒数N(N<86400) 输出: 当前时刻N秒之前的时间,以hh:mm:ss格式。
输入样例1:
06:17:22
80
输出样例1:
06:16:02
输入样例2:
00:00:01
2
输出样例2:
23:59:59
代码:
C++:
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main() {
string s; cin >> s;
int h1 = (s[0] - '0') * 10 + (s[1] - '0');
int m1 = (s[3] - '0') * 10 + (s[4] - '0');
int s1 = (s[6] - '0') * 10 + (s[7] - '0');
int num;
cin >> num;
int res = h1 * 3600 + m1 * 60 + s1 - num;
if (res >= 0) {
int h2 = res / 3600;
int m2 = res % 3600 / 60;
int s2 = res - h2 * 3600 - m2 * 60;
printf("%02d:%02d:%02d", h2, m2, s2);
}
else {
res = 86400 - (num - h1 * 3600 - m1 * 60 - s1);
int h2 = res / 3600;
int m2 = res % 3600 / 60;
int s2 = res - h2 * 3600 - m2 * 60;
printf("%02d:%02d:%02d", h2, m2, s2);
}
return 0;
}
/*
06:17:22
80
06:16:02
*/
Python:
class Solution:
def main(self):
n = list(input().split(":"))
res = int(n[0]) * 3600 + int(n[1]) * 60 + int(n[2])
cost = int(input())
res = res - cost
if res >= 0:
h = res // 3600
m = (res - h * 3600) // 60
s = res - h * 3600 - m * 60
print("%02.d:%02.d:%02.d" % (h, m, s))
else:
res = 86400 + res
h = res // 3600
m = (res - h * 3600) // 60
s = res - h * 3600 - m * 60
print("%02.d:%02.d:%02.d" % (h, m, s))
if __name__ == "__main__":
s = Solution()
s.main()
Java:
package com.my.gududu;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int h1 = (s.charAt(0) - '0') * 10 + (s.charAt(1) - '0');
int m1 = (s.charAt(3) - '0') * 10 + (s.charAt(4) - '0');
int s1 = (s.charAt(6) - '0') * 10 + (s.charAt(7) - '0');
int num = input.nextInt();
int res = h1 * 3600 + m1 * 60 + s1 - num;
if (res >= 0) {
int h2 = res / 3600;
int m2 = res % 3600 / 60;
int s2 = res - h2 * 3600 - m2 * 60;
System.out.println(String.format("%02d:%02d:%02d", h2, m2, s2));
}
else {
res = 86400 - (num - h1 * 3600 - m1 * 60 - s1);
int h2 = res / 3600;
int m2 = res % 3600 / 60;
int s2 = res - h2 * 3600 - m2 * 60;
System.out.println(String.format("%02d:%02d:%02d", h2, m2, s2));
}
}
}