【每天一道算法题】Numeric Keypad
题目描述
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.
输入例子:
32583131
输出例子:
2580129
剑指offer公司真题部分,微软的题目。
#include <iostream> #include <vector> #include <string> #include <queue> using namespace std; int pad[10][10] = {{ 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 }}; int last[] = { 0,9,6,2,6,4,1,3,2,0 };string maxnum(string& s1) {string s2;s2.push_back(s1[0]);int len = s1.length();for (int i = 1; i < len; ) {int need = s1[i] - '0';int key = s2.back() - '0';int j = 0;for (j = last[key]; j >= 0; j--){if (pad[key][j] == need) {i++;s2.push_back(pad[key][j] + '0');break;}}if (j < 0) {for (j = last[key]; j >= 0; j--){if (pad[key][j] < need) {s2.push_back(pad[key][j] + '0');key = s2.back() - '0';for (int j = s2.size(); j < len; j++)s2.push_back(pad[key][last[key]] + '0');return s2;}}}if (j < 0) {need = key;s2.pop_back();if (s2.size() == 0) {s2.push_back(need - 1 + '0');key = s2.back() - '0';for (int j = s2.size(); j < len; j++)s2.push_back(pad[key][last[key]] + '0');return s2;}key = s2.back()-'0';for (j = last[key]; j >= 0; j--){if (pad[key][j] < need) {s2.push_back(pad[key][j] + '0');key = s2.back() - '0';for (int j = s2.size(); j < len; j++)s2.push_back(pad[key][last[key]]+'0');return s2;}}}}return s2; }int main() {int num;cin >> num;vector<string> vec(num,"");for (int i = 0; i < num; i++)cin >> vec[i];for (int i = 0; i < num; i++)cout << maxnum(vec[i]) << endl;return 0; }
转载于:https://www.cnblogs.com/LUO77/p/5833524.html
【每天一道算法题】Numeric Keypad相关推荐
- 一天一道算法题--5.30---递归
感谢微信平台: 一天一道算法题 --------每天多一点进步--------- 今天 休假 这题 也很水 能发现 规律就好 明天 再也不能这样了 forgive me ---------- 转载于 ...
- 一天一道算法题--6.15--卡特兰数
感谢微信平台---一天一道算法题---每天多一点进步- problem: 12个高矮不同的人 排成两排 每排必须是从矮到高排列 而且第二行比对应的第一排的人高 问排列方式有多少种? analyse: ...
- 一道算法题跟大家分享
无意中看到一道算法题,拿出我的解法与大家分享. 题目:输出1,2,3,4四个数字能组成的互不相同且无重复的三位数并统计满足条件的三位数个数 大家先想想,再看答案吧. View Code 1 /// & ...
- 每天一道算法题系列十七之电话号码的字母组合
每天一道算法题系列: 来源:力扣(LeetCode) 本题链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-numb ...
- 搜狗2020秋招笔试的一道算法题
搜狗2020秋招的一道算法题 有A,B,C三种样品,凑齐三个样品各一个就可以领取一个奖品.任意两个样品可兑换另一种样品,如AA可兑换一个B或一个C,AB可兑换一个C.输入ABC,问最多可以领取多少奖品 ...
- 每天一道算法题系列十二之整数转罗马数字
每天一道算法题系列: 来源:力扣(LeetCode) 本题链接:https://leetcode-cn.com/problems/integer-to-roman/ 来源是力扣,大家喜欢可以去力扣中文 ...
- 截止目前为止,我遇到的最难的一道算法题:计算相邻两个数的最大差值
hello,今天给大家带来一道算法题.这道算法题,是我目前为止,见过最难的一道题.那么到底是怎样的一道算法题呢?如下: 题目:给定一个数组, 求如果排序之后, 相邻两数的最大差值. 要求时间复杂度O( ...
- Homebrew作者面试Google被拒,只因写不出一道算法题
相信很多人听说过关于Max Howell(Homebrew的作者)的故事: Max Howell在Google面试,但Google拒绝了他,给出的答复是:"虽然我们90%的工程师都用你写的软 ...
- 最佳买卖股票时间 Java_一道算法题的讲解-买卖股票的最佳时间
今天讲解一道简单的算法题: 问题描述 假设我们有一个数组, 数组中按顺序每个元素的值表示当天的股票价格. 例如,数组: [7,1,5] 表示股票第一天是7元,第二天是1元,第三天是5元 约定,只能买一 ...
最新文章
- mybatis学习5复杂查询之多对一的处理
- 7-12 藏头诗 (15 分)
- FPGA中亚稳态——让你无处可逃
- python最简单的爬取邮箱地址怎么写_详解python定时简单爬取网页新闻存入数据库并发送邮件...
- mysql数据库操作常用命令_MySQL数据库操作常用命令小结
- [转载] hexo categories和tags页面不显示解决办法
- Unity实现刺客信条灯光的思路探究
- box-shadow详解
- 初中英语语法(006)-特殊疑问句·祈使句·感叹句
- 全国青少年软件编程(Scratch)等级考试一级考试真题2022年12月——持续更新.....
- iOS 开发之使用 Facebook POP
- linux mmc 读写,linux内核mmc读写分析
- 凯恩帝1000对刀图解_凯恩帝数控机床对刀方法
- RS导出Excel交叉表角对应的列占用多列问题
- Netty handler无法进行依赖注入的问题
- ACM入门及STL简介(转)
- VBA 模块级变量和过程级变量,全局变量,局部变量,end,exit end sub等影响
- 相机成像(镜头与小孔成像)
- RAFT: Recurrent All-Pairs Field Transforms for Optical Flow
- matlab电力系统潮流计算,基于MATLAB的电力系统潮流计算
热门文章
- mysql integrityerror_mysql插入数据报错IntegrityError: (1062, Duplicate entry 'xx' for key 'xxxxx')...
- 学习记录1-python图片分割transforms方法
- unity人物旋转移动代码_游戏诞生之日02 - 美术篇 快速制作人物动画
- Google Guava 库用法整理
- 西伯利亚(Siberia)冷空气
- 初玩PLSQL连接 Oracle
- 记录 spring 使用@Value获取properties文件中的属性值
- ubuntu 配置minicom 进行串口开发
- C语言实现顺序栈心得
- 手工编程:hello world