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)

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 程序猿接私活经验总结,来自csdn论坛语录

    程序猿接私活经验总结,来自csdn论坛语录

    2021年11月15日
  • docker 导入导出镜像_docker拉取镜像到本地

    docker 导入导出镜像_docker拉取镜像到本地docker导入导出镜像文件:把某个docker镜像保存到本地文件,命令如下dockersave-o镜像名.tar原始镜像名(REPOSITORY项)导出$dockersave-o/root/images/jenkins_image.tarjenkins/jenkins:latest导入$dockerload</root/images/jenkins_image.tar导出镜像如果要存出镜像到本地文件,可以使用dockersave命令。例如,存出本地的ubu

  • Python源码剖析_python编程300例pdf

    Python源码剖析_python编程300例pdf关注“Java后端技术全栈”回复“面试”获取全套面试资料Python是一种跨平台的计算机程序设计语言,是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。Python最初被设…

  • Eclipse如何安装svn插件及使用「建议收藏」

    Eclipse如何安装svn插件及使用「建议收藏」Eclipse中使用SVN此文章对Myeclipse同样适用。一.在Eclipse里下载Subclipse插件方法一:从EclipseMarketplace里面下载具体操作:打开Eclipse–&gt;Help–&gt;EclipseMarketplace–&gt;在Find中输入subclipse搜索–&gt;找到subclipse点击in…

  • Promise.all的深入理解「建议收藏」

    Promise.all的深入理解「建议收藏」异步之PromisePromise.allPromise.all接收的promise数组是按顺序执行的还是一起执行的,也就是说返回的结果是顺序固定的吗?目前有两种答案:应该是同步执行的,但是这样就有效率问题了,如果想改成异步执行怎么办呢?有些人认为结果是按顺序执行的,有些人认为结果顺序不确定。那么我们根据实现来解密:环境为:vscode1.20….

  • ES6数组去重的三个简单办法

    ES6数组去重的三个简单办法ES6数组去重的三个简单办法简单说一下利用ES6实现数组去重的三个办法。第一种:利用Map对象和数组的filter方法贴上相关代码打印后的结果通过打印我们发现,确实实现了我们想要的效果。那么下面简单来解释一下。1.Map对象是ES6提供的一个新的数据结构,其中has的办法是返回一个布尔值,表示某个值是否存在当前的Mp对象之中,set的办法是给Map对象设置key/value。2…

发表回复

您的电子邮箱地址不会被公开。

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