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


相关推荐

  • MATLAB循环_matlab如何循环计算

    MATLAB循环_matlab如何循环计算MATLAB循环1.   while循环   在给定条件为真时,重复一个语句或一组语句。它在执行循环体之前测试状态。语法MATLAB中while循环的语法是:while&lt;expression&gt;  &lt;statements&gt;end只要表达式(expression)为true,while循环将重复执行程序语句(statements)。当结果为非空并且…

    2022年10月7日
    0
  • 详解马氏距离中的协方差矩阵计算(超详细)

    详解马氏距离中的协方差矩阵计算(超详细)一、概率统计基本知识1.样本均值样本均值(Mean)是在总体中的样本数据的平均值。2.样本方差方差(Variance)是度量一组数据的离散(波动)程度。方差是各个样本与样本均值的差的平方和的均值,分母除以n-1是为了满足无偏估计:3.样本标准差4.协方差协方差(Covariance)是度量两个变量的变动的同步程度,也就是度量两个变量线性相关性程度。若协方差大于0,表示一个变量增大时另一个变量也会增大,即两个变量呈正相关;若协方差小于0,表示一个变量增大时另一个变量会减小,即两个变量呈负相关

    2022年6月28日
    51
  • android 空间分享到朋友圈,Android开发之微信分享到好友,朋友圈

    android 空间分享到朋友圈,Android开发之微信分享到好友,朋友圈3.快速集成第二步:配置AndroidManifest.xml下面清单文件的配置是全部的,没有的平台就是不需要配置1、添加权限2、添加activity信息(注意:tencent后面的appid要保持和您配置的QQ的appid一致)android:name=”com.mob.tools.MobUIShell”android:theme=”@android:style/Theme.Transluc…

    2022年5月20日
    77
  • 2015-11-01 HTML, CSS, JavaScript, DOM

    2015-11-01 HTML, CSS, JavaScript, DOM

    2021年9月10日
    47
  • pycharm加注释的快捷方式_pycharm如何批量注释

    pycharm加注释的快捷方式_pycharm如何批量注释1、主题  在开发项目的过程中经常需要创建任务列表,对于一些小任务,在代码中插入便签注释会有奇效。  这种注释的格式如下:  2、创建便签注释  非常简单:  首先,将光标定位到相应行。  然后,创建一行注释(Ctrl+Slash)。  最后,在#后面,输入TODO或者todo,然后输入注释  3、浏览便签  在T

    2022年8月28日
    0
  • C++ this指针

    C++ this指针this指针引言:首先,我们都知道类的成员函数可以访问类的数据(限定符只是限定于类外的一些操作,类内的一切对于成员函数来说都是透明的),那么成员函数如何知道哪个对象的数据成员要被操作呢,原因在于每个对象都拥有一个指针:this指针,通过this指针来访问自己的地址。注意:this指针并不是对象的一部分,this指针所占的内存大小是不会反应在sizeof操作符上的。this指针的类型取决于…

    2022年5月17日
    37

发表回复

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

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