日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【每天一道算法题】Numeric Keypad

發(fā)布時間:2024/4/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【每天一道算法题】Numeric Keypad 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

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.

?

輸入例子:
3
25
83
131

?

輸出例子:
25
80
129
劍指offer公司真題部分,微軟的題目。 根據(jù)題意,可以知道的是,按下某個鍵后,這個鍵左方及上方的鍵不能再用了。例如按下了[5]:



所以我們可以得到一張表,以表示按下某個鍵后還剩下的合法按鍵:


一個數(shù)組表示key,也就是當前被查的字符。 一個數(shù)組last表示,當前被查字符的可用數(shù)字的個數(shù)。例如1是9,0是0,9是0。 例如來一個131. 把1輸入進去。 從3開始查。need=3; 會有以下三種情況: 找到need。。如果找到,那么查找下一個字符,也就是1. 找不到need。。我試著找第一個小于need的數(shù)。那么后面的所以數(shù)位則填充當前最大的合法值,結(jié)束,返回; 連小于need的數(shù)我都找不到。。就像這個例子,我在key=3的時候,找need=1。我根本找不到,這個時候,需要回到key=1的時候(需要pop,遇到字符串為空的情況需要特殊處理),找1的可用數(shù)組里,比need稍微小一點的(不能直接找need-1,因為need-1可能不在可用數(shù)組里。),那么后面的所以數(shù)位則填充當前最大的合法值,結(jié)束,返回。
#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; }

?









轉(zhuǎn)載于:https://www.cnblogs.com/LUO77/p/5833524.html

總結(jié)

以上是生活随笔為你收集整理的【每天一道算法题】Numeric Keypad的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。