题目描述很恶心,明显在内涵国家前领导人,建议小米在审核方面用点心。(不了解的自行谷歌)
下面进行题目分析:
没啥难的,简单分析下就可以得知,先根据给定时间长度计算出时分相同的次数:
1、分钟数大于、等于小时数:天数*24+小时数+1
2、分钟数小于小时数:天数*24+小时数+1-1
由此得到减少的秒数,下面就是时间转换。
不多说,直接贴代码:
<?php
$line = trim(fgets(STDIN));
$arr = explode(' ', $line);
$arr_1 = explode('d', $arr[0]);
$day = (int)$arr_1[0];
$hour = (int)$arr_1[1];
$minute = (int)$arr[1];
$second = (int)$arr[2];
if($hour > $minute){
$res = 1;
}elseif($hour == $minute){
if($second == 59){
$res = 0;
}else{
$res = 1;
}
}else{
$res = 0;
}
$sec = $day * 24 + $hour + 1 - $res;
$original_second = getSec($day, $hour, $minute, $second);
$end_second = $original_second - $sec;
$string = getTime($end_second);
echo $string;
return $string;
//返回秒数
function getSec($day, $hour, $minute, $second)
{
$second = $day * 24 * 60 * 60 + $hour * 60 * 60 + $minute * 60 + $second;
return $second;
}
//根据秒数,返回时间
function getTime($sec)
{
$day = floor($sec / (24 * 60 * 60));
$hour = floor(($sec % (24 * 60 * 60)) / (60 * 60));
$minute = floor((($sec % (24 * 60 * 60)) % (60 * 60)) / 60);
$second = (($sec % (24 * 60 * 60)) % (60 * 60)) % 60;
return $day . 'd' . changeStr($hour) . ' ' . changeStr($minute) . ' ' . changeStr($second);
}
function changeStr($str)
{
$str = (string)$str;
if(strlen($str) == 1){
$str = '0'.$str;
}
return $str;
}