#P1945. 第1题-小红的时钟
-
1000ms
Tried: 148
Accepted: 28
Difficulty: 3
所属公司 :
百度
时间 :2024年8月25日
-
算法标签>模拟
第1题-小红的时钟
思路
简单数学,计算小时,分钟,秒钟的差之后 / 60即可得到秒钟转的圈数。
代码
python
# 不保证time_1 < time_2,需要考虑隔天的情况
def calc_second (time_1 , time_2):
time_1 = time_1.split(":")
time_2 = time_2.split(":")
h1 , m1 , s1 = int(time_1[0]) , int(time_1[1]) , int(time_1[2])
h2 , m2 , s2 = int(time_2[0]) , int(time_2[1]) , int(time_2[2])
#考虑隔天的情况
if h1 > h2 or (h1 == h2 and m1 > m2) or (h1 == h2 and m1 == m2 and s1 > s2):
h2 += 24
second = (h2 - h1) * 3600 + (m2 - m1) * 60 + (s2 - s1)
return second
n = int(input())
time_arr = input().split()
res = []
for i in range(n - 1):
tmp = calc_second(time_arr[i] , time_arr[i + 1]) / 60
# 强制保留两位小数 , 不过的补0
res.append(f"{tmp:.2f}")
print(" ".join(map(str , res)))
C++
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
vector<string> spliString(string s,char del){
vector<string> res;
stringstream ss(s);
string t;
while(getline(ss,t,del)){
res.push_back(t);
}
return res;
}
int main(){
long long n;
cin>>n;
string pre;
cin>>pre;
char del = ':';
n--;
while(n--){
vector<string> ptime = spliString(pre, del);
int phour = stoi(ptime[0]);
int pmin = stoi(ptime[1]);
int pst = stoi(ptime[2]);
string nex;
cin>>nex;
vector<string> ntime = spliString(nex, del);
int nhour = stoi(ntime[0]);
int nmin = stoi(ntime[1]);
int nst = stoi(ntime[2]);
if(nhour<phour){
nhour+=24;
if(nmin<pmin){
nhour--;
nmin+=60;
}
if(nst<pst){
nmin--;
nst+=60;
}
}
if(nhour==phour&&nmin<pmin){
nhour+=24;
if(nmin<pmin){
nhour--;
nmin+=60;
}
if(nst<pst){
nmin--;
nst+=60;
}
}
if(nhour==phour&&nmin==pmin&&nst<pst){
nhour+=24;
if(nmin<pmin){
nhour--;
nmin+=60;
}
if(nst<pst){
nmin--;
nst+=60;
}
}
int sums = (nhour-phour)*3600+(nmin-pmin)*60+nst-pst;
double cil = sums/60.00;
cout<<fixed<<setprecision(2)<<cil<<" ";
pre = nex;
}
}
OJ会员可以通过点击题目上方《已通过》查看其他通过代码来学习。
题目内容
小红的寝室里放着一个时钟。时钟有时针,分针,秒针三种指针。在某些时刻,时钟会记录下当前时间,格式为hh:mm:ss。时钟上看不出日期,每天零点,三根指针都会归零。现在小红得到了一系列被记录的时间,且这些时间是按照先后顺序被记录的。小红想让你算算,每两个时间点之间,秒针至少转了多少圈。 注:一天有24个小时,1小时有60分钟,1分钟有60秒,秒针一分钟转一圈。
输入描述
第一行一个整数n(2≤n≤105),代表时间序列中时间点的个数。
第二行n个字符串,每个字符串代表一个时间点,格式为hh:mm:ss(0≤hh<24,0≤mm<60,0≤ss<60)。
输出描述
n−1个数,第i个数表示第i~i+1时间点秒针转过的圈数。保留两位小数
示例1
输入
2
10:00:00 10:00:30
输出
0.50
说明
从10:00:00至10:00:30至少过了半分钟,秒针在时钟上走了半圈
示例2
输入
3
10:00:00 09:00:00 08:00:00
输出
1380.00 1380.00
说明
从10:00:00至09:00:00至少过了23小时,秒针在时钟上走了1380圈
从09:00:00至08:00:00至少过了23小时,秒针在时钟上走了1380圈
示例3
输入
2
00:00:00 00:00:00
输出
0.00