一道微软笔试题

题目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
</pre><pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;
char s[10000];
int rr[10]={3,0,0,0,1,1,1,2,2,2};
int ll[10]={1,0,1,2,0,1,2,0,1,2};
char ans[10000];
int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int i,j,k,t,T;
    int n,m;
    scanf("%d",&T);
    for(t=1;t<=T;t++){
        scanf("%s",s);
        int len=strlen(s);
        for(i=0,j=0;i<len;i++){
            if(s[i]!='0') break;
        }
        for(j=0;i<len;i++){
            s[j++]=s[i];
        }
		len=j ; 
		s[j]=0;
        if(len==1) {
            printf("%s\n",s);
            continue;
        }
        for(i=0;i<len;i++) s[i]=s[i]-'0';
        for(i=0;i<len-1;i++){
            if(ll[s[i+1]]>=ll[s[i]] && rr[s[i+1]]>=rr[s[i]]){
                ans[i]=s[i];
                if(i==len-2){
                    ans[i+1]=s[i+1];
                }
            }
            else {
                while(s[i+1]>0){
                    if(ll[s[i+1]]>=ll[s[i]] && rr[s[i+1]]>=rr[s[i]])break;
                    else s[i+1]--;
                }
                //s[i+1]=0;
                if(ll[s[i+1]]>=ll[s[i]] && rr[s[i+1]]>=rr[s[i]]){
                    ans[i]=s[i]; ans[i+1]=s[i+1];
                    if(s[i+1]>0)
                        for(j=i+2;j<len;j++)ans[j]=9;
                    else for(j=i+2;j<len;j++)ans[j]=0;
                    break;
                }else{
                    s[i]=s[i]-1;
                    //ans[i]=s[i]-1;
                    //if(ans[i]>0)
                        for(j=i+1;j<len;j++)s[j]=9;
                    //else for(j=i+1;j<len;j++)ans[j]=0;
                    if(i==0)i=-1;
                    else i-=2;
                    //break;
                }
            }
        }
        for(i=0;i<len;i++)if(ans[i]) break;
        for(i;i<len;i++)printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值