3

If you start a 12-hour analog clock at a given time, and stop at another given time, how many times will the minute hand overtake the hour hand?

More or less I know how to do it, but what are those special cases ?

I have something like this:

#include<cstdio>
#include<cmath>

int main()
{
int t,h1,m1,i,h2,m2,count,j,j1,j2;
scanf("%d",&t);
while(t--)
{
scanf("%d:%d",&h1,&m1);
scanf("%d:%d",&h2,&m2);
if(h1==0 && m1==0)
  m1+=1;
count=0;
if(h1==h2)
{
  if(h1>=12)
      j=(60*(h1-12))/11;
  else
      j=(60*h1)/11;
  if(j>=m1 && j<m2)
   count++;
}
else
{
  for(i=h1+1;i<h2;i++)
  {
    if(i!=11 && i!=23)
     count++;
  }

  if(h1>=12)
      j1=(60*(h1-12))/11;
  else
      j1=(60*h1)/11;
  if(j1>=m1 && j1<=59)
   count++;

  if(h2>=12)
      j2=(60*(h2-12))/11;
  else
      j2=(60*h2)/11;
  if(j2<m2 && j2<=59)
   count++;
}
printf("%d\n",count);
}
return 0;
}

But, my code for some tests returns a bad result :(

For example:

22:00 02:00

My code should return 3, but it returns 18

anatolyg
  • 26,506
  • 9
  • 60
  • 134
xaxa
  • 31
  • 2
  • Could you please provide more details on your question? I don't understand what you're asking. What is a "tip"? What does it mean for one to "coincide with an hour until a specific time"? What "special cases" are you referring to? – user3553031 Oct 17 '15 at 19:41
  • The idea is to calculate how many times will cover tips minute of the hour from 00:00 to specified by us hours – xaxa Oct 17 '15 at 19:44
  • I still don't understand what you're talking about. What does "cover tips minute of the hour" mean? – user3553031 Oct 17 '15 at 19:48
  • overlap clockwise, I mean this – xaxa Oct 17 '15 at 19:50
  • Oh, you mean that if you start a 12-hour analog clock at 00:00, how many times will the minute hand overtake the hour hand before a given time is reached? – user3553031 Oct 17 '15 at 19:52
  • Yes, I mean, and for example: I start on 00:00 and for 23:59 we have 23 overlap clockwise – xaxa Oct 17 '15 at 19:54
  • @xaxa What "special cases" are you referring to? – anatolyg Oct 18 '15 at 14:27

2 Answers2

1

Here is one algorithm that uses the difference in angular velocity between the minute hand and the hour hand:

#include <iostream>
#include <cmath>
using namespace std;

int main(){
  int H1, M1, H2, M2, answer;
  double time1, time2;
  char smiec;

  while (cin >> H1 >> smiec >> M1 >> H2 >> smiec >> M2){
    H2 = H2 < H1 ? H2 + 24 : H2;

    time1 = H1 + M1/60.0;

    time2 = H2 + M2/60.0;

    answer = floor(11/12.0 * time2) - floor(11/12.0 * time1);

    cout << answer << endl;
  }
}
גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • I guess this version calculates a correct result. If so, how does it work? Also, please specify where the problem in OP's code is, and how this version fixes it (if possible). – anatolyg Oct 18 '15 at 07:58
  • BTW [you should not use `bits/stdc++.h`](http://stackoverflow.com/q/31816095/509868), especially in a teaching example. – anatolyg Oct 18 '15 at 08:01
  • @anatolyg Thank you for your comments. I know nothing about C++ so I had to look up what your comment meant. Does my updated answer help you better understand? – גלעד ברקן Oct 18 '15 at 22:42
0

The intersections will occur at: 12:00, 01:05, 02:10, 03:16, 04:21, 05:27, 06:32, 07:38, 08:43, 09:49, 10:54.

I think a better approach would be to count over time list if they intersect or not. First normalizing your input data, after making the algorithm that counts it. Otherwise, you should dig into the geometry and math underluing it the problem in your equation.

The approach of counting would lead to a similar optmized algorithm, since you have a max range of no more than 24 hours, which has a constant time for every input.

If otherwise you want to still using this approach, your code would be better by checking if the constraint of T1 be always less than T2. For example:

int H1, M1, H2, M2;
double vd, vm, ratio, t, t1, t2, spot;
char smiec;

while (cin >> H1 >> smiec >>M1 >> H2 >> smiec >> M2)
{
    ratio = 720 / 11;

    t1 = H1 * 60 + M1;
    t2 = H2 * 60 + M2;

    if (t2 < t1)
        t2 += 24 * 60;

    t = t2 - t1;

    spot = floor(abs(t / ratio));

    cout << spot << endl;
}
thyago stall
  • 1,654
  • 3
  • 16
  • 30
  • Not quite. If we start at 00:00, then the hour hand will be at 01:00 by the time that the minute hand gets back up to the top. It will take another five minutes for the minute hand to get to 01:00, during which time the hour hand will have moved. You could get the correct answer by solving a recursive function -- or you could just divide 12 hours by 11. – user3553031 Oct 17 '15 at 20:25
  • So, not quite, why? because, the cover clockwise after 1:05 with hook – xaxa Oct 17 '15 at 20:36
  • You are right. But I the main the problem on the suggested algorithm is that it should normalize the input data. The example of 22:00 to 2:00, its computing the absolute differente, which is 10 hours and not 4 as one would intuitively answer. – thyago stall Oct 17 '15 at 20:37
  • The correct answer is 4 hours. But if you compute the absolute difference between 22 and 2, it results in 10. 2 should be added of 24 to result the desired answer. – thyago stall Oct 17 '15 at 20:45
  • So, to correct your algorithm: when the time 2 (H2 and M2) is smaller than time 1, you should add 24 hours. I would suggest you to restrict the input to a time range of 0-24 or 0-12 hours, so you can always guarantee the correct result. – thyago stall Oct 17 '15 at 20:49
  • So, for example 13:05 13:06 my you code and my code return 0, should return 1 – xaxa Oct 17 '15 at 21:08
  • That's why a better approach would be using a list of the times where it happens that the clock hands overlap. Using the list, you just count how many of the possible times are inside the given interval. – thyago stall Oct 17 '15 at 21:09
  • 01:05 01:05 should return 0 01:06 01:06 should return 0 01:05 01:06 should return 1 – xaxa Oct 17 '15 at 21:11