您的位置:首页 > 其它

微软2016校园招聘在线笔试第二场

2015-04-24 22:03 316 查看


题目2 : Numeric Keypad

时间限制:10000ms
单点时限:1000ms
内存限制:256MB


描述

The numberic keypad on your mobile phone looks like below:
1 2 3
4 5 6
7 8 9
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) move your 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 <= 10500, 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


#include<iostream>
#include<string>
#include<vector>
using namespace std;

int inside(int k, vector<int> nextstep)
{
int flag;
int len = nextstep.size();
int i;
if (k < nextstep[0])
return -1;
for (i = 0; i < len; i++)
{

if (k<nextstep[i])
break;
else
flag = nextstep[i];
}

return flag;
}

int maxiNum(vector<vector<int>> next,string K)
{
int res=0;
int len = K.length();
int flag = 0;
int i,j;
int temp;
j=1;
for (i = 0; i < len; i++)
{
temp = inside(K[i] - '0', next[j]);

if (flag == 1)
{
res *= 10;
j = next[j][next[j].size() - 1];
res += j;
}
else if (temp == -1)
{
do{
temp = res % 10-1;
res /= 10;

if ((inside(temp, next[res%10]))!=-1||res==0)
break;
else
i--;
} while (1);
res *= 10;
res += temp;
flag = 1;
i--;
}
else
{
res *= 10;
j = temp;
res += j;
if (temp != K[i] - '0')
{
flag = 1;
}
}

}

return res;
}

int main()
{
int T;
string K;
vector<int> res;
int i;

vector<vector<int>> next;
vector<int> nextstep;

nextstep.push_back(0);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(1);
nextstep.push_back(2);
nextstep.push_back(3);
nextstep.push_back(4);
nextstep.push_back(5);
nextstep.push_back(6);
nextstep.push_back(7);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(2);
nextstep.push_back(3);
nextstep.push_back(5);
nextstep.push_back(6);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(3);
nextstep.push_back(6);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(4);
nextstep.push_back(5);
nextstep.push_back(6);
nextstep.push_back(7);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(5);
nextstep.push_back(6);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(6);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(7);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(0);
nextstep.push_back(8);
nextstep.push_back(9);
next.push_back(nextstep);

nextstep.clear();
nextstep.push_back(9);
next.push_back(nextstep);

cin >> T;
for (i = 0; i < T; i++)
{
cin >> K;
res.push_back(maxiNum(next,K));
}
for (i = 0; i < T; i++)
cout << res[i] << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: