Digital Clock 數論水題

Description
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

題意:電子時鐘顯示時間09:24:31相當於數字92431,問給定時間段內有多少個時間是3的倍數

解:初始化一下,統計到每個時間未知有多少個3的倍數,之後就是查詢就可以了

<span style="font-size:18px;">#include <stdio.h>
#include <string.h>
int sh,sm,ss;
int eh,em,es;
int time[235999];
void init()
{
    memset(time,0,sizeof(time));
    int maxn=0;
    sh=sm=ss=0;
    while(sh!=24)
    {
        int flag=0;
        if((sh+sm+ss)%3==0)
        {
            time[sh*10000+sm*100+ss]=maxn+1;
            maxn++;
        }
        if((ss+1)%60==0)
        {
            ss=0;
            flag=1;
        }
        else
            ss++;
        if(flag)
        {
            if((sm+1)%60==0)
            {
                sm=0;
                flag=1;
            }
            else
            {
                sm++;
                flag=0;
            }
        }
        if(flag)
        {
            sh++;
        }
    }
    maxn=0;
    for(int i=0;i<=235959;i++)
    {
        if(!time[i])
            time[i]=maxn;
        else
            maxn=time[i];
    }
    return ;
}
int main()
{
    int t;
    int i;
    int sum;
    init();
    while(scanf("%d",&t)!=-1)
    {
        while(t--)
        {
            scanf("%d:%d:%d %d:%d:%d",&sh,&sm,&ss,&eh,&em,&es);
            sum=time[eh*10000+em*100+es]-time[sh*10000+sm*100+ss];
            if(sum<0)
                sum+=time[235959];
            if((sh+sm+ss)%3==0)            //端點
                sum++;
            printf("%d\n",sum);
        }
    }
    return 0;
}
</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章