Good morning!
InterviewTime limit1sMemory limit256 MB
Find the nonnegative integer closest to k whose consecutive keypad digits never move up or left, breaking ties toward the smaller number.
- Level
Easy3 of 10
- Topics
- Brute force, Implementation
- Solved
- No attempts yet
Problem
Good morning! This is your 5 a.m. wake-up call. A partly cloudy day is coming, with light rain in the afternoon.
You have just woken up. You desperately need coffee. And more coffee. And some cereal. And your clothes. And coffee.
To make warm cereal you put some milk in the microwave and heat it for seconds. That means typing on the microwave keypad, which looks like this:
1 2 3
4 5 6
7 8 9
0
The digits 1 to 9 sit in 3 rows and 3 columns, and 0 sits directly below 8.
You still have not had your coffee, so your hand keeps falling down, and so do your eyes and your brain. You can only type a number while your hand moves down or to the right. You cannot go back left and you cannot move your hand up, though you can press the same key again. And again. And again.
In other words, read the typed number from the left: between two neighbouring digits, neither the row nor the column of the key may decrease. You can type 180 or 49, but not 98 or 132.
Type the number closest to among the numbers you can type. The number you type is a nonnegative integer, and a number of two or more digits does not start with 0. If two numbers are equally close, type the smaller one.
Input
The first line contains the number of test cases ().
Each of the next lines contains one integer ().
Output
For each test case, print on its own line the number closest to that can be typed on the keypad. If two numbers are equally close, print the smaller one.