计算机挑战赛8

有一时钟在某时刻的时间为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));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值