Numeric Keypad 解题报告

描述
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 <=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
解题思路:首先要注意的是输入的k是字符串,不能按整数处理(我也被坑过)。
1、这题可以用dfs做,每次递归深入一层,递归的目的是找到需要减小的数字的位置L,L之前的数字不变,L处的字符降一位,L之后的数字全部置为L处新数字所有可匹配的数字中的最大值,即得结果。
2、当前数字的可匹配数字是指当前数字之后可以按的数字。

#include <iostream>
#include <string>
using namespace std;
//m数组记录每个数字的可匹配数
int m[10][11]={{0,-1},{9,8,7,6,5,4,3,2,1,0,-1},{9,8,6,5,3,2,0,-1},{9,6,3,-1},{9,8,7,6,5,4,0,-1},{9,8,6,5,0,-1},{9,6,-1},{9,8,7,0,-1},{9,8,0,-1},{9,-1}};
//mm数组记录每个数字可以匹配的最大最小值
int mm[10][2]={{0,0},{9,0},{9,0},{9,3},{9,0},{9,0},{9,6},{9,0},{9,0},{9,9}};
string num;
int len;
//ismatch()用来判断两个数字是否可匹配
bool ismatch(int a,int b){
    for(int i=0;m[a][i]!=-1;i++){
        if(m[a][i]==b)return true;
    }
    return false;
}
//dfs每一层,从第一个数字,即第0层开始
int dfs(int k){
    if(k==len)return k;   //扫描完所有数字,结束递归
    int m;
    if(k==0){             //扫描第一个数字,特殊处理,不要要和前一个数字匹配
        m=dfs(k+1);       
        if(m==-1)return 0;    //如果下一层需要降位,但是已经最小,需要返回-1,让上一位数字降位
        return m;             
    }
    int temp=num[k-1]-'0';
    if(num[k]-'0'<mm[temp][1])return -1;
    if(!ismatch(num[k-1]-'0',num[k]-'0'))return k;
    m=dfs(k+1);
    if(m==-1){
        if(num[k]-'0'>mm[temp][1])
        return k;
    else 
        return -1;
    }
    return m;
}
//确定L后,输出最后结果
void output(int k){
    if(k==len){
        cout<<num<<endl;
        return;
    }
    if(k==0){
        num[k]=num[k]-1;
    }else{
        for(int i=0;i<11;i++){
            if(m[num[k-1]-'0'][i]<num[k]-'0'){
                num[k]=m[num[k-1]-'0'][i]+'0';
                break;
                }
            }
     }
    int temp=num[k]-'0';
    temp=mm[temp][0];
    for(int i=k+1;i<len;i++){
        num[i]=temp+'0';
    }
    cout<<num<<endl;
}
int main(){
    int n;
    cin>>n;
    while(n--){
        cin>>num;
        len=num.size();
        int k=dfs(0);
        output(k);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值