2000 Canadian Computing Competition, Stage 1

Problem J2: 9966

The digits 0, 1, and 8 look much the same if rotated 180 degrees on the page (turned upside down). Also, the digit 6 looks much like a 9, and vice versa, when rotated 180 degrees on the page. A multi-digit number may also look like itself when rotated on the page; for example 9966 and 10801 do, but 999 and 1234 do not.

You are to write a program to count how many numbers from a given interval look like themselves when rotated 180 degrees on the page. For example, in the interval [1..100] there are six : 1, 8, 11, 69, 88, and 96.

Your program should take as input two integers, m and n, which define the interval to be checked, 1 ≤ mn ≤ 32000. The output from your program is the number of rotatable numbers in the interval.

You may assume that all input is valid.

Sample Input

1
100

Sample Output

6

All Submissions
Best Solutions


Point Value: 3
Time Limit: 2.00s
Memory Limit: 16M
Added: Oct 01, 2008

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

I tested this out on my computer but it seemed to work. When I submitted this to the OJ it gave me an WA. :(

You are not null-terminating reverse, hence your if (reverse == num) check will always fail.

The sample input says the two numbers will be on the same line. I was getting RE so I tried inputs on separate lines and it worked. The sample information can throw people off.


Any hint why my latest submission for this question is wrong? My code passed all test cases offline. But it got all wrong answers here. Any hint?

print "The number of rotatable numbers is:" before printing the actual answer...

Test data links were broken, probably due to a change in the server's file structure. It's fixed now, and submissions are rejudged.

Is not the two input integers should be on the same line? Just as the Sample Input?

In the actual input, the two integers may be on different lines.

For future reference, it's best if you read comments other people have written to see if somebody's already answered your question.

Specifically, you seem to have assumed that m and n will always be given on a single line, whereas in fact the problem statement does not guarantee this, and it's possible for them to be given on different lines.

A small question:
would the number 80 be classified as rotatable? you could represent it as 080?

Interesting idea, but nope. Just do a regular check of the given number.

For example, in the interval [1..100] there are six : 1, 8, 11, 69, 88, and 96.
In programming problems, unless explicitly stated otherwise, leading zeroes are not allowed in numbers.

I checked a lot of test cases which I verified by hand and I'm getting all the correct answers. However, when I submit my program, all the test cases return "Exception_in_thread_..." What exactly does this refer to? Thanks.

That's a runtime error; an exception occurred, meaning something unexpected went wrong. Sadly Java happens to be very verbose, so that the part of the message that actually tells you what you did wrong was cut off. You got a NoSuchElementException in java.util.StringTokenizer, which means an error occurred while parsing input. Specifically, you seem to have assumed that m and n will always be given on a single line, whereas in fact the problem statement does not guarantee this, and it's possible for them to be given on different lines. Consider using java.util.scanner instead.

Thanks a lot Brian! I knew that an exception occurred, I just didn't know where or which exception occurred because the output ended with "..."

Thanks again for your help :).

How do people do this question? It's hard. Help? (I can't believe this question's worth 3 points. Some people say it's the hardest 3-point question they've ever done)

It's rather simple once you think about it.
Simply [redacted]. That specifically is probably the hardest part, but there are many ways you can do it.