Numeric Keypad

Numeric Keypad题目描述Thenumberickeypadonyourmobilephonelookslikebelow:123456789 0 supposeyouareholdingyourmobilephonewithsinglehand.Yourthumbpointsatdigit1.Eachtimeyoucan1)press

大家好,又见面了,我是你们的朋友全栈君。

题目描述

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 thumbpoints at digit 1. Each time you can 1)press the digit your thumbpointing at.2)moveyour thumb right,3)move your thumb down. Moving yourthumb left or up is not allowed.

 By using the numeric keypad under above constrains, you can producesome numbers like 177 or 480 while producing other numbers like 590 or52 is impossible.

 Given a number K, find out the maximum number less than or equal to Kthat 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.

输入例子:
3
25
83
131

输出例子:
25
80
129

代码演示:

# -*- encoding: utf8 -*-
"""
二维列表pad表示当前行值对应的数字下一次可以按的数字列在第二维度上
一维列表last表示当前数字的下一次按键可用数字数-1,方便循环进行了-1

举例解析:
数字:131
把1输入,
从3开始查找,有3种情况:
 1) 找到need,那么查找下一个字符
 2) 找不到need,那么试着去找第一个小于need的数,之后的数字就选择当前最大的合法值,结束并返回
 3) 连小于need的数都找不到,就像这个例子:在key=3的时候,找need=1,找不到,这个时候,
需要回到key=1的时候(去掉ret最后的字符,字符串变空需要特殊处理),找1可用的数组里比3小的
数字(不能直接-1,因为可能不在可用范围内)。之后的数字填充最大的合法值,结束并返回。
"""

# mapping table
pad = [[0],
		   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
		   [0, 2, 3, 5, 6, 8, 9],
		   [3, 6, 9],
		   [0, 4, 5, 6, 7, 8, 9],
		   [0, 5, 6, 8, 9],
		   [6, 9],
		   [0, 7, 8, 9],
		   [0, 8, 9],
		   [9]]
last = [0, 9, 6, 2, 6, 4, 1, 3, 2, 0]

def find_min_number(num):
	"""
	find the minimum number
	"""
	ret = []
	input_str = str(num)
	input_length = len(input_str)
	ret.append(input_str[0])
	i = 1
	while i < input_length:
		need = int(input_str[i])
		key = int(ret[-1])
		j = last[key]
		# Situation 1)
		while j >= 0:
			if pad[key][j] == need:
				i += 1
				ret.append(str(pad[key][j]))
				break
			j -= 1 

		# Situation 2)
		if j < 0:
			j = last[key]
			while j >= 0:
				if pad[key][j] < need:
					ret.append(str(pad[key][j]))
					key = int(ret[-1])
					return ''.join(ret) + str(pad[key][last[key]])*(input_length-len(ret))
				j -= 1

		# Situation 3)
		if j < 0:
			need = key
			ret = ret[0:-1]
			if len(ret) == 0:
				ret.append(str(need-1))
				key = int(ret[-1])
				return ''.join(ret) + str(pad[key][last[key]])*(input_length-len(ret))

			key = int(ret[-1])
			j = last[key]
			while j >= 0:
				if pad[key][j] < need:
					ret.append(str(pad[key][j]))
					key = int(ret[-1])
					return ''.join(ret) + str(pad[key][last[key]])*(input_length-len(ret))
				j -= 1
	return ''.join(ret)

T = raw_input()
intT = int(T)

for i in range(intT):
	n = raw_input()
	num = int(n)
	print find_min_number(num)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132495.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月10日 下午7:00
下一篇 2022年6月10日 下午7:00


相关推荐

  • idea 好用插件_效率软件推荐

    idea 好用插件_效率软件推荐0.引言不同类型的开发插件具备不同的作用,有能够直接提升我们开发效率的,也有能够规范我们代码的。在茫茫的插件海中,结合我这些年的使用总结,给大家推荐几款IDEA插件1.代码规范类1.1AlibabaJavaCodingGuidelines介绍首当其冲的必定是阿里的代码规范插件AlibabaJavaCodingGuidelines,从我还是小白时到现在这款插件已经陪伴了我几年光阴。我愿称它为你最少最少要遵守的代码规范。安装后,当你书写的代码不符合阿里规范时,会将代码标黄,鼠标移动

    2025年9月7日
    14
  • 智慧小区解决方案ppt_智慧小区简介

    智慧小区解决方案ppt_智慧小区简介智慧小区项目遇到的问题汇总&解决参考跨域问题mybatisplus操作问题git操作问题跨域问题前端使用vue脚手架搭建项目,后端使用springboot+MySQL,首当其冲的问题是两者不能使用同一个端口启动,这就涉及到跨域操作。事实上,第一步,要在vue项目中的vue.config.js里添加//跨域parallel:require(‘os’).cpus().length>1,pwa:{},devServer:{port:8081,

    2022年10月17日
    5
  • 【回溯法】--01背包问题

    【回溯法】--01背包问题回溯法 01 背包问题 1 问题描述 给定 n 种物品和一背包 物品 i 的重量是 wi gt 0 其价值为 vi gt 0 背包的容量为 c 问应如何选择装入背包中的物品 使得装入背包中物品的总价值最大 要求使用回溯法 例如 算法分析 整体思路 01 背包属于找最优解问题 用回溯法需要构造解的子集树 对于每一个物品 i 对于该物品只有选与不选 2 个决策 总共有 n 个物品 可以顺序依次考虑每个物品 这

    2026年3月26日
    1
  • 冒泡法排序_冒泡选择排序算法

    冒泡法排序_冒泡选择排序算法/*冒泡法排序:共进行N-1趟比较,每趟比较中要进行N-1-i次两两比较时间复杂度:最差、平均都是O(n^2),最好是O(n)空间复杂度:O(1)稳定排序*/

    2022年10月18日
    4
  • solid原则应用实例_设计模式solid原则

    solid原则应用实例_设计模式solid原则在面向对象编程中,SOLID是5个重要的设计原则的缩写。首先是由著名的软件大师RobertC.Martin(Bob大叔)在DesignPrinciplesandDesignPatterns中提出,后来MichaelFeathers用SOLID来概括这五大原则。SOLID原则使得软件设计更加容易理解、灵活和可维护。作为一名软件工程师,这5个原则我们必须知道。本文,我将涵盖这些原则,并举例说明怎样是违背了原则,以及如何进行纠正来符合SOLID原则。S—Singlerespons

    2025年7月12日
    6
  • 《深入理解mybatis原理》 MyBatis的架构设计以及实例分析

    《深入理解mybatis原理》 MyBatis的架构设计以及实例分析MyBatis 是目前非常流行的 ORM 框架 它的功能很强大 然而其实现却比较简单 优雅 本文主要讲述 MyBatis 的架构设计思路 并且讨论 MyBatis 的几个核心部件 然后结合一个 select 查询实例 深入代码 来探究 MyBatis 的实现

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号