跳到正文

题解

题解:SP9046 HANDS - Clock Hands

QWQ_SczyYA约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年7月16日
收录
2026年7月16日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
3
题目
SP9046

版本

  1. v2当前
  2. v1

SP9046 HANDS - Clock Hands

分析

因为分针与时针自 00:0000:00 起第 kk 次重合发生在 tk=720k11t_k=\dfrac{720k}{11} 分钟处(k=0,…,22k=0,\dots,22)。
所以对于给定区间 (start,end](start,end] 内的时针分针重合次数,就等价于统计满足 11⋅start<720k≤11⋅end11\cdot start < 720k \le 11\cdot end 的整数 kk 个数,直接枚举 k=0…22k=0\dots 22 并判断是否合法即可。

复杂度

由于每个测试用例只需要枚举常数个 kk(最多 2323 个),所以时间复杂度为 O(T)O(T)。

C++ 参考代码

C++
#include <bits/stdc++.h>
using namespace std;
int t;
void slv(){
    string s1, s2;
    cin>>s1>>s2;
    int h1=stoi(s1.substr(0,2)),m1=stoi(s1.substr(3,2)),h2=stoi(s2.substr(0,2)),m2=stoi(s2.substr(3,2)); //用substr截取字符串中的小时和分钟,stoi将string转int来读入起始的时间。
    int start=h1*60+m1,end=h2*60+m2,cnt=0; //start和end意思如上面的分析,cnt记录合法的k的数量。
    for(int k=0;k<=22;k++)
        if(start*11<720*k&&720*k<=end*11) cnt++; //合法则加
    cout<<cnt<<'\n';
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0); //cin,cout加速
    cin>>t;
    while(t--) slv(); //t组数据
    return 0;
}
原文
存档评论3 条抓取于