算法十一

时间

算法描述

  • 计算机操作者习惯于使用一个数字表示当前的日期及时间(甚至精确到毫秒)
  • 你在此可以写一个方法,计算出任意数字的秒对应的时间
  • 比如,从凌晨开始走了3661秒,则计算出的时间为1时1分1秒,你的输出为”1:1:1”

参数定义

  • 类名 Time
  • 方法 whatTime
  • 输入参数 int
  • 输出 string
  • 方法声明 string whatTime(int seconds)

限制条件

  • seconds 在0到24*60*60 - 1 = 86399 之间

例子

  • 输入
    • seconds: 0
  • 输出
    • 0

测试实例

  • 实例一

    • 输入

      • 3661
    • 输出

      • 1:1:1
  • 实例二

    • 输入
      • 5436
    • 输出
      • 1:30:36
  • 实例三

    • 输入
      • 86399
    • 输出
      • 23:59:59

代码

#include <iostream>

using namespace std;

class Time {
public:
    string whatTime(int seconds) {
        // calculate the hour
        int hour = seconds / 3600;
        // calculate the minute
        int minute = (seconds % 3600) / 60;
        // left is the second
        int secLast = seconds - hour * 3600 - minute * 60;
        // formated the three integer value to "*:*:*" string
        return to_string(hour) + ":" + to_string(minute) + ":" + to_string(secLast);
    }
};

int main() {
    int seconds = 86399;
    Time tm;
    string ret = tm.whatTime(seconds);
    cout << ret << endl;
    cout.flush();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值