秒を時:分:秒に
整数 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));