1 条题解
-
0
核心逻辑
将总秒数转换为“时:分:秒”格式(xx:xx:xx),步骤如下:
- 计算小时:总秒数 ÷ 3600(1小时=3600秒);
- 计算剩余秒数:总秒数 % 3600;
- 计算分钟:剩余秒数 ÷ 60(1分钟=60秒);
- 计算秒:剩余秒数 % 60;
- 每个部分需补零至两位数(如1→01,5→05)。
代码实现
C
#include <stdio.h> int main() { int total_seconds; scanf("%d", &total_seconds); int hours = total_seconds / 3600; int remaining = total_seconds % 3600; int minutes = remaining / 60; int seconds = remaining % 60; // %02d确保输出两位数,不足补零 printf("%02d:%02d:%02d\n", hours, minutes, seconds); return 0; }C++
#include <bits/stdc++.h> using namespace std; int main() { int total_seconds; cin >> total_seconds; int hours = total_seconds / 3600; int remaining = total_seconds % 3600; int minutes = remaining / 60; int seconds = remaining % 60; // 格式化输出,补零至两位数 cout << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes << ":" << setw(2) << setfill('0') << seconds << endl; return 0; }Python
total_seconds = int(input()) hours = total_seconds // 3600 remaining = total_seconds % 3600 minutes = remaining // 60 seconds = remaining % 60 # 用:02d确保两位数,补零 print(f"{hours:02d}:{minutes:02d}:{seconds:02d}")验证(样例输入
3701)- 小时:3701 ÷ 3600 = 1
- 剩余秒数:3701 % 3600 = 101
- 分钟:101 ÷ 60 = 1
- 秒:101 % 60 = 41
输出01:01:41,与样例一致。
所有输入均按规则转换,确保格式为xx:xx:xx。
- 1
信息
- ID
- 446
- 时间
- 1000ms
- 内存
- 64MiB
- 难度
- 7
- 标签
- 递交数
- 147
- 已通过
- 38
- 上传者