秒数转时:分:秒
给出整数 s(秒)。
把这段时间按 时:分:秒 的格式输出。
分和秒总是写成两位,只有一位时前面补 0(5 → 05)。
时不补 0,按原样输出,即使达到 24 或以上也不进位成天。
- 输入
- 0 ≤ s ≤ 10,000,000
- 输出
- 一行 H:MM:SS(例:1:02:05、0:00:45、27:46:40)
3725
1:02:05
45
0:00:45
给出整数 s(秒)。
把这段时间按 时:分:秒 的格式输出。
分和秒总是写成两位,只有一位时前面补 0(5 → 05)。
时不补 0,按原样输出,即使达到 24 或以上也不进位成天。
3725
1:02:05
45
0:00:45
运行时这些行会依次作为 input()/标准输入传入(运行中不会等待输入)。
$ 就绪
请在电脑上打开以编写和运行代码。在手机上可以查看题目和题解。
时 = s / 3600,分 = s % 3600 / 60,秒 = s % 60。
只用 s / 60 求分钟,超过一小时就会得到 60 以上。
整数除法:C、C++、Java、C# 中两个 int 的 / 已经是商,Python 用 //,JS/TS 需要 Math.floor,Craykon 用 数学.商(s, 3600)。
最常见的陷阱是忘了补 0,输出 1:2:5。
补成两位:C/Java %02d,Python f"{m:02d}",C# {m:D2},JS/TS String(m).padStart(2, "0"),C++ std::setfill('0') << std::setw(2)——setfill 会一直保留,但 setw 只对紧接着的一个值有效,所以秒前面要再写一次。
Craykon 没有补位工具,写一个小函数:如果 (x < 10) 就在前面加 "0"。
s = int(input())h = s // 3600m = s % 3600 // 60sec = s % 60print(f"{h}:{m:02d}:{sec:02d}")const s = Number(require("fs").readFileSync(0, "utf8").trim());const h = Math.floor(s / 3600);const m = Math.floor((s % 3600) / 60);const sec = s % 60;const two = (x) => String(x).padStart(2, "0");console.log(`${h}:${two(m)}:${two(sec)}`);import { readFileSync } from "node:fs";const s: number = Number(readFileSync(0, "utf8").trim());const h: number = Math.floor(s / 3600);const m: number = Math.floor((s % 3600) / 60);const sec: number = s % 60;const two = (x: number): string => String(x).padStart(2, "0");console.log(`${h}:${two(m)}:${two(sec)}`);import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); int h = s / 3600; int m = s % 3600 / 60; int sec = s % 60; System.out.printf("%d:%02d:%02d%n", h, m, sec); }}using System; class Program{ static void Main() { int s = int.Parse(Console.ReadLine().Trim()); int h = s / 3600; int m = s % 3600 / 60; int sec = s % 60; Console.WriteLine($"{h}:{m:D2}:{sec:D2}"); }}#include <iomanip>#include <iostream> int main() { int s; std::cin >> s; int h = s / 3600; int m = s % 3600 / 60; int sec = s % 60; std::cout << h << ':' << std::setfill('0') << std::setw(2) << m << ':' << std::setw(2) << sec << '\n';}#include <stdio.h> int main(void) { int s; scanf("%d", &s); int h = s / 3600; int m = s % 3600 / 60; int sec = s % 60; printf("%d:%02d:%02d\n", h, m, sec); return 0;}두자리(x){ 如果 (x < 10) { 返回 "0" + x; } 返回 "" + x;} s = 控制台.读行().转数字();h = 数学.商(s, 3600);m = 数学.商(s % 3600, 60);sec = s % 60;控制台.输出(h + ":" + 두자리(m) + ":" + 두자리(sec));