首页 > ACM题库 > HDU-杭电 > Hdu 1371 Clock-模拟[解题报告] C++
2013
12-09

Hdu 1371 Clock-模拟[解题报告] C++

Clock

问题描述 :

You are given a standard 12-hour clock with analog display, an hour hand and a minute hand. How many times does the minute hand pass the hour hand in a given time interval?

输入:

The input contains an indefinite number of lines; each line contains four numbers.The first pair of numbers represents an “initial time” the second pair represents a “final time.”

In each such number pair, the first number represents hours, second number represents minutes.

The hours will be in the range 1..12, the minutes in the range 0..59.

No initial time and no final time will be an instant at which the minute hand just passes the hour hand. (In particular, 12 00 will not occur as an initial or final time.)

No initial time will be the same as the corresponding final time.

Between each initial time and corresponding final time, the hour hand will have turned less than one full revolution (360 degrees).

As the hour hand turns from its initial position to its final position, it may or may not sweep past the number 12 on the clock’s dial.

If it does, then either the initial time is an “A.M.” time and the final time a “P.M.” time, or vice versa.

If it does not, then either both times (initial and final) are “A.M.” or both are “P.M.”

Each line of input gives rise to one line of output, containing

the initial and final times, and

the number of times the minute hand passes the hour hand between the initial time and the final time.

Observe all details of formatting, such as upper/lower case letters, punctuation, blank spaces, and the absence of blank lines.

In each time display, the hours and minutes are displayed in fields of width 2, separated by a colon.

The ten’s digit (of hours or minutes) is displayed as a zero if it is zero.

输出:

Here is a formatting template shown between two lines of the above output:Initial time Final time Passes
12345678901234567890123456789012
12:50 01:02 0

样例输入:

12 50  1  2
 3  8  3 20
 2 45 11  0
11  0  3 20
 1  2 12 50
 3 20  3  8

样例输出:

Program 3 by team X
Initial time  Final time  Passes
       12:50       01:02       0
       03:08       03:20       1
       02:45       11:00       8
       11:00       03:20       4
       01:02       12:50      11
       03:20       03:08      10
End of program 3 by team X


YOYO’s Note:
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄它是华丽的分隔线

【题意简述】

给定一个开始时间和一个结束时间(12小时进制),求这段时间内时针与分针重叠的次数。

【粗略分析】

先画一下草稿就知道差不多一小时5分钟多一点会重叠一次,
于是我把所有的点算出来 – - ||,然后判断该时间是否在开始与结束时间之内。
是则inc,最后输出结果即可。

【C++源代码】

#include<iostream>
using namespace std;

bool in(int sh,int sm,int eh,int em,int h,int m)
{
    if(sh>h)
        return false;
    if(sh==h&&sm>m)
        return false;
    if(eh<h)
        return false;
    if(eh==h&&em<=m)
        return false;
    return true;
}

int Turn(int sh,int sm,int eh,int em)
{
    int p=0;
    if(eh<sh)eh+=12;
    if(eh==sh&&em<sm)eh+=12;
    if(eh>=24)eh-=12,sh-=12;
    if(in(sh,sm,eh,em,0,0))p++;        //00:00:00
    if(in(sh,sm,eh,em,1,5))p++;        //01:05:27
    if(in(sh,sm,eh,em,2,10))p++;    //02:10:54
    if(in(sh,sm,eh,em,3,16))p++;    //03:16:21
    if(in(sh,sm,eh,em,4,21))p++;    //04:21:49
    if(in(sh,sm,eh,em,5,27))p++;    //05:27:16
    if(in(sh,sm,eh,em,6,32))p++;    //06:32:43
    if(in(sh,sm,eh,em,7,38))p++;    //07:38:10
    if(in(sh,sm,eh,em,8,43))p++;    //08:43:38
    if(in(sh,sm,eh,em,9,49))p++;    //09:49:05
    if(in(sh,sm,eh,em,10,54))p++;    //10:54:32
    if(in(sh,sm,eh,em,12,0))p++;    //12:00:00
    if(in(sh,sm,eh,em,13,5))p++;    //13:05:27
    if(in(sh,sm,eh,em,14,10))p++;    //14:10:54
    if(in(sh,sm,eh,em,15,16))p++;    //15:16:21
    if(in(sh,sm,eh,em,16,21))p++;    //16:21:49
    if(in(sh,sm,eh,em,17,27))p++;    //17:27:16
    if(in(sh,sm,eh,em,18,32))p++;    //18:32:43
    if(in(sh,sm,eh,em,19,38))p++;    //19:38:10
    if(in(sh,sm,eh,em,20,43))p++;    //20:43:38
    if(in(sh,sm,eh,em,21,49))p++;    //21:49:05
    if(in(sh,sm,eh,em,22,54))p++;    //22:54:32
    return p;
}

int main()
{
    int sh,sm,eh,em,p;
    cout<<"Program 3 by team X"<<endl;
    cout<<"Initial time  Final time  Passes"<<endl;
    while(cin>>sh>>sm>>eh>>em)
    {
        cout<<"       ";
        if(sh<10) cout<<0;
        cout<<sh<<":";
        if(sm<10) cout<<0;
        cout<<sm;
        cout<<"       ";
        if(eh<10) cout<<0;
        cout<<eh<<":";
        if(em<10) cout<<0;
        cout<<em<<"  ";
        p=Turn(sh,sm,eh,em);
        printf("%6d ",p);
    }
    cout<<"End of program 3 by team X"<<endl;
    return 0;
}

 

【注意事项】

※ 输出格式啊输出格式

【点评】

其实细一点就好勒 = =
因为OJ会吞空格,所以伤掉了两次PE……

转载自:http://blog.csdn.net/yahreso/article/details/2344883


  1. 题目需要求解的是最小值,而且没有考虑可能存在环,比如
    0 0 0 0 0
    1 1 1 1 0
    1 0 0 0 0
    1 0 1 0 1
    1 0 0 0 0
    会陷入死循环