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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle未明确定义列[通俗易懂]

    oracle未明确定义列[通俗易懂]分类:SQL使用技巧2012-04-0616:121332人阅读评论(0)收藏举报运行环境:Oracle10gsqlplus环境下。 在查询语句中,经常会出现一个错误: SQL基础:ORA-00918:未明确定义列的错误。 当前遇到有两种情况。原因为:当查询语句中,查询的表(数据集)中有相同的字

    2022年10月5日
    6
  • 关于DetailsView使用DropDownList1!

    关于DetailsView使用DropDownList1!关于DetailsView使用DropDownList1在DetailsView中创建一个模板列,在模板列中加入DropDownList例:<asp:TemplateFieldHeaderText=”类型”><EditItemTemplate><asp:DropDownListID=”DropDownList2″runat=”ser…

    2022年7月18日
    16
  • 中国地图china.js[通俗易懂]

    中国地图china.js[通俗易懂]中国地图china.js一、简介中国地图china是基于echarts.js和china.js绘制图像。官方已不支持china.js下载china.js:https://static.delebug.com/echarts/china.js二、配置项//china.js的配置项与echarts基本图形配置项相通//关于echarts基本图形配置参考:https://echarts.apache.org/v4/zh/option.html//其中china地图主要配置不同处在seri

    2022年7月20日
    25
  • 阿里云设置DDNS(动态域名解析)

    阿里云设置DDNS(动态域名解析)阿里云设置DDNS(动态域名解析)搭建内网服务器时,因为运营商分配的公网ip地址是动态的。在一段时间后或者重启路光猫后,会导致公网ip变化,此时阿里云设置DNS将失效。因此需要进行动态域名解析。阿里云没有像花生壳一样的内置到路由器的动态域名解析服务。所以,我们没办法在路由器段进行动态域名解析设置。但是,阿里云提供了DNS的API,各个语言的API都有,因此我们可以在服务器端来实现这个动态域名解析服务。下面讲一下我实现的整个过程,我是通过go语言完成的。如下。1.设置DNS域名解析服务进入阿里云的

    2022年6月7日
    51
  • Spring Cloud Eureka服务注册中心 多节点搭建(学习总结)

    Spring Cloud Eureka服务注册中心 多节点搭建(学习总结)一、前言:本文主要搭建SpringCloudEureka服务注册中心(多节点),本文基于SpringBoot1.5.2,SpringCloudCamden.SR6版本编写,版本不一致可能会有差异。下面就学习总结记录一下:二、搭建Eureka-Server首先,引入相应的依赖pom.xml:<?xmlversion=”1.0″encoding=”UTF-8″?…

    2022年10月8日
    4
  • XML: 使用XmlDocument 与 XmlReader 类

    XML: 使用XmlDocument 与 XmlReader 类一.XmlDocument类:XmlDocument与XmlReader类从.NET1.0就已经存在了。W3C定义了一个叫做文件对象模型(DOM:DocumentObjectModel)的标准来处理XML文档。支持DOM的类可以自由地定位并修改XML文档。要想使用XmlDocument类,需要添加System.Xml.dll的引用,并且引入System.Xml命名空间。XmlDocu

    2022年6月19日
    31

发表回复

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

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