首页 > 试题广场 >

Numeric Keypad

[编程题]Numeric Keypad
  • 热度指数:7833 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
The numberic keypad on your mobile phone looks like below:
123
456
789
0
suppose you are holding your mobile phone with single hand. Your thumb points at digit 1. Each time you can 1)press the digit your thumb pointing at.2)moveyour thumb right,3)move your thumb down. Moving your thumb left or up is not allowed.
By using the numeric keypad under above constrains, you can produce some numbers like 177 or 480 while producing other numbers like 590 or 52 is impossible.
Given a number K, find out the maximum number less than or equal to K that can be produced.

输入描述:
the first line contains an integer T, the number of testcases.
Each testcase occupies a single line with an integer K.

For 50%of the data ,1<=K<=999.
For 100% of the data, 1<=K<=10^500,t<=20.


输出描述:
for each testcase output one line, the maximum number less than or equal to the corresponding K that can be produced.
示例1

输入

3
25
83
131

输出

25
80
129
头像 贪吃的迪恩顶呱呱
发表于 2024-05-09 19:33:41
一、首先将每个数的位置坐标记录在表中方便查阅注意题目给的有问题,实际矩阵应为:1 2 34 5 67 8 9 0即:0是在第二列的位置,而不是题目给的第一列二、通过模拟一个数生成的过程来检验一个数是否合法例子:如果要生成58,则遍历字符串,5的行列值是[2,2],8的行列值是[3,2],那么符合 展开全文
头像 _Bingbong
发表于 2024-12-31 01:35:50
解题思路 这是一个数字构造问题,使用DFS解决: 预处理可达数字: 对于每个位置,预处理可以到达的所有数字 从大到小排序,便于找到最大可行解 DFS策略: 从高位到低位逐位构造 使用limit标记是否受到上界限制 当找到第一个可行解时立即返回 剪枝优化: 对于每个位置,只考虑不 展开全文

问题信息

难度:
55条回答 13285浏览

热门推荐

通过挑战的用户

查看代码
Numeric Keypad