A contest closes in n days hh hours, mm minutes and ss seconds. Given two values of n, how many palindromes of the format nhhmmss would we find in the indicated interval?
A string is said to be palindrome if it reads the same backwards as forwards.Here is what I tried,... would anyone please explain how should I proceed
#include<stdio.h>
int ifpalin(int g)
{
int rev=0;
int tmp=g;
while(tmp>0)
{
rev=rev*10+(tmp%10);
tmp=tmp/10;
}
if(rev==g)
return 1;
else
return 0;
}
int findpalin(int a1,int b1)
{
int sm=0;
for(int i=a1;i<=b1;i++)
{
if (ifpalin(i)==1)
sm++;
}
printf("%d",sm);
printf("\n");
return 0;
}
int main()
{
int a,b,n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a);
scanf("%d",&b);
findpalin(a,b);
}
return 0;
}
A contest closes in n days hh hours, mm minutes and ss seconds. Given two values of n, how many palindromes of the format nhhmmss would we find in the indicated interval?
A string is said to be palindrome if it reads the same backwards as forwards.
Constraints n2 - n1 <= 10
Input One line containing two integer n1 and n2, where n1< p="">
Output One integer representing the number of palindromes in this countdown interval
Time Limit 3
Examples Example 1
Input
1 2
Output
472
Explanation
We need to check the numbers 1000000 through 2235959 including only numbers where the last 6 digits correspond to times. We find 472 such numbers: 1000001, 1001001, 1002001, 1003001, 1004001, ..., 2231322, 2232322, 2233322, 2234322, 2235322
Example 2
Input
0 2
Output
708
Explanation
There are 708 palindromes: 0000000, 0001000, 0002000, 0003000, 0004000, ..., 2231322, 2232322, 2233322, 2234322, 2235322
0000000 //this is a valid time? Is this a 24 hour clock?
I still think you can cook up a formula and get the count without a search/generate/count.
but I clearly missed all the single digit and internal pattern ones, have to find a way to determine those.
or at the very least, generate 000 to 999 table of true/false for whether it is both a valid palindrome and a valid time, use that lookup.
yes this is a 24-hr clock and I gave the example above... I thought that formula could be related to fact that for 0 and 1 as input it gives 236 (708-472) and for 1 & 2 answer is 472.
yea you are all over it... its a pat answer depending on the hour.
I mean its fugly, but you can just find the answer for 00 to 23 and put that number in a table. Then you just sum those up over the hours you were given. It may turn out that there are only like 3 or 4 values depending on which hour it is. I dunno. Is this one of those idiot site problems where they want you to do a billion cases in a second?
Explanation
We need to check the numbers 1000000 through 2235959 including only numbers where the last 6 digits correspond to times. We find 472 such numbers: 1000001, 1001001, 1002001, 1003001, 1004001, ..., 2231322, 2232322, 2233322, 2234322, 2235322
I'm obviously missing something; I came up with
2 x 24 x 6
which is 288, not 472.
Can you explain the count and then maybe the logic will be clearer.
I am not going to code it for you. You need to count them, though.
count how many you get in a 24 hour period. Then use the Ns to compute the answer.
the first example may just tell you: if you get 472 for 1, 2 then for 1, 3 you get twice that, right? Programs like this often have 2 programs: one to figure out part of the answer, like how many in an hour or 24 hours or whatever, and another that uses the result of the first to answer the bigger question quickly (once you know the magic number, you can just apply it in the second program, you don't need to find that again).
I don't get 472 either. I stopped trying, I can't make sense of it. I get 6 per hour:
000, 010, 020, 030,040,050 //example for hour == 0
where these are the middle values:
NH hMm Ss
only hMm vary
M is from 0-5
m and h are equal and are 0-9
so 24 hours is 24*6 = 144 per day. I think this matches lastchance's approach.
Let's assume a single digit for n. This is in direct conflict with the OP's statement:
Constraints n2 - n1 <= 10
Which would allow n2 = 12345677899 and n1 = 1234567895
Anyway, if n is a single digit then write the values like Jonnin did:
NHhMmSs
Separating into handy groups:
N Hh M mS s
So N matches s, Hh matches mS and M is in the middle.
N is however many days you're talking about and s can be any digit, so they always match
Hh is 00-23, but it must match mS and S can only be in the range 0-5. So there are 8 illegal values of Hh (06 07 08 09 16 17 18 19). Thus there are 24-8=16 legal values of HhEdit: all values of Hh are legal, see Jonnin's post above and comment below.
M can be any value from 0-5.
So for each day there are 16*6=96 24*6=144 legal values and the full answer is N*96 N*144. This gives 192 288 palindromes for the first example (N=2) and 288 432 for the second example.
The full set for the first example. If the real answer is 472 then there should be plenty that I missed. Does anyone see any? Edit: see Jonnin's comment below
shrutichandel, it looks like the question is crap since it can't even get correct results for the examples. I'd move on to something else, preferably from a different site. Maybe pick some of the homework questions that have been posed here and see if you can do them.
Host of these "programming" sites are pretty useless for beginners because finding the algorithm is much harder than writing the code.