int findArithSeqs(int nMins) // Find # Arithm Seqs within nMins minutes
{
int nSeqs = 0;
int hr = 12; // Start at 12:00
int min = 0;
// Check every reading of the digital clock
for ( int i = 0; i < nMins; i++ )
{
min++; // This can make min >= 60 !
adjust hr, min to HH:MM, where 1 < HR < 12, 1 < MM < 59
if ( hr < 10 )
{
Time = a:bc
if ( a, b, c is an arithm. seqs )
nSeqs++;
}
else
{
Time = ab:cd
if ( a, b, c, d is an arithm. seqs )
nSeqs++;
}
}
return nSeqs;
}
|
int findArithSeqs(int nMins) // Find # Arithm Seqs within nMins minutes
{
int nSeqs = 0; // # arithm seqs found
/* --------------------------------------------------
Start time at 12:00
-------------------------------------------------- */
int hr = 12; // hr and min simulate the 12 hr clock...
int min = 0;
int a, b, c, d; // a,b,c,d are the digits on the clock...
/* ---------------------------------------------
Check time from 12:00 to 12:00 + nMins
--------------------------------------------- */
for ( int i = 1; i <= nMins; i++ )
{
min++; // next time in minutes
// Adjust clock
if ( min == 60 )
{
hr++;
if ( hr == 13 )
hr = 1;
min = 0;
}
// Check for arithm. seq
if ( hr < 10 )
{ // Time is 3 digits
a = hr; b = min/10; c = min%10;
if ( (a-b) == (b-c) )
{
nSeqs++;
}
}
else
{ // Time is 4 digits
a = hr/10; b = hr%10; c = min/10; d = min%10;
if ( ((a-b) == (b-c)) && ((a-b) == (c-d)) )
{
nSeqs++;
}
}
}
return nSeqs;
}
|
Observe that the digital clock is periodic:
12:00 --> 1:00 --> 2:00 --> .... --> 11:00 --> 12:00 --> 1:00 ...
Each period is 12 hrs = 12*60 mins
12:00
|---------------------------------------------------------------|
<--------------------------- D mins -------------------------->
12:00 12:00 12:00 12:00 12:00
|--------------|--------------|--------------|--------------|---|
R
D = #periods + R
# periods = D/(12*60)
R = D%(12*60)
So:
findArithSeqs(D) = D/(12*60) * findArithSeqs(12*60)
+ findArithSeqs(D%(12*60))
|
DEMO: CCC/2017/Progs/j4.cc