ZOJ.3131 Digital Clock【打表】 2015/10/11

Digital Clock

Time Limit: 1 Second      Memory Limit: 32768 KB

Digital clocks usually show the time in the form hh:mm:ss, where hh is a number between 00 and 23, and both mm and ss are numbers between 00 and 59. Removing the colons from hh:mm:ss will result in an integer hhmmss, which is called a clock integer. For example, the clock integer of 17:05:13 is 170513 and the clock integer of 00:07:37 is 737.

You are given a time interval and you are to determine the number of clock integers in it that are multiples of 3. A time interval will be given by specifying its start and end time. For example, the time interval [00:59:58, 01:01:24] has a total of 1+1+60+25=87 clock integers, namely, 5958, 5959, 10000 through 10059, and 10100 through 10124. How many of them are multiples of 3?

Note that a time interval that includes midnight may have a start time greater than its end time, as in [22:47:03, 01:03:24]. You may assume that a time interval is at least one second long and shorter than 24 hours.

Write a program that can determine the number of multiples of 3 in a time interval.

Input

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case consists of a single line that contains the start time and end time of a time interval, which are separated by a single space.

Output

Your program is to write to standard output. Each test case outputs exactly one line. Print the number of multiples of 3 among the clock integers in the time interval. The following shows a sample input with three test cases and its output.

Sample Input

3
00:59:58 01:01:24
22:47:03 01:03:24
00:00:09 00:03:37

Sample Output

29
2727
70

Source: Asia Regional Contest Seoul 2006

打表做,记录从0:00:00到当前时刻符合要求的时刻的数量,如果是第一天到第二天,分两次求

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

int times[300000];

void init(){
    memset(times,0,sizeof(times));
    int i,j,k;
    times[0] = 1;
    for( i = 1 ; i <= 235959 ; ++i ){
        if( i % 100 >= 60 || i % 10000 >= 6000 )
            times[i] = -1;
        else{
            if( i % 10000 == 0 ){
                times[i] = times[i-4041];
                if( i % 3 == 0 )
                    times[i]++;
            }
            else if( i % 100 == 0 ){
                times[i] = times[i-41];
                if( i % 3 == 0 )
                    times[i]++;
            }
            else{
                times[i] = times[i-1];
                if( i % 3 == 0 )
                    times[i]++;
            }
        }
    }
}

int main(){
    int h,h1,h2,m,m1,m2,s,s1,s2;
    int t,ans;
    int ret1,ret2;
    init();
    cin>>t;
    while(t--){
        scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
        ret1 = h1 * 10000 + m1 * 100 + s1;
        ret2 = h2 * 10000 + m2 * 100 + s2;
        if( ret1 > ret2 ){
            ans = times[235959] - times[ret1];
            ans += times[ret2];
        }
        else ans = times[ret2] - times[ret1];
        if( ret1 % 3 ==0 )
            ans++;
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值