Задача 5.Messages от More Exercise Introduction Java - Малко помощ?
Здравейте, може ли малко помощ на тази задача :
Write a program, which emulates typing an SMS, following this guide:
1 |
2 abc |
3 def |
4 ghi |
5 jkl |
6 mno |
7 pqrs |
8 tuv |
9 wxyz |
0 space |
Following the guide, 2 becomes “a”, 22 becomes “b” and so on.
Examples
Input |
Output |
|
Input |
Output |
|
Input |
Output |
5 44 33 555 555 666 |
hello |
9 44 33 999 0 8 44 33 777 33 |
hey there |
7 6 33 33 8 0 6 33 |
meet me |
Hints
- A native approach would be to just put all the possible combinations of digits in a giant switch statement.
- A cleverer approach would be to come up with a mathematical formula, which converts a number to its alphabet representation:
Digit |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- Let’s take the number 222 (c) for example. Our algorithm would look like this:
- Find the number of digits the number has “e.g. 222 è 3 digits”
- Find the main digit of the number “e.g. 222 è 2”
- Find the offset of the number. To do that, you can use the formula: (main digit - 2) * 3
- If the main digit is 8 or 9, we need to add 1 to the offset, since the digits 7 and 9 have 4 letters each
- Finally, find the letter index (a è 0, c è 2, etc.). To do that, we can use the following formula: (offset + digit length - 1).
- After we’ve found the letter index, we can just add that to the ASCII code of the lowercase letter “a” (97)
И трите Zero Testove минават и след това се чупи някъде. Моля някой за малко съвет?
Ето до къде съм стигнал: https://pastebin.com/8A191U9b
Това е линк към JUdge: https://judge.softuni.bg/Contests/Practice/Index/1461#4
Благодаря, колега! От цял ден решаване явно съм зациклил накрая.