Hihocoder #1153 Numeric Keypad

Source: Internet
Author: User

Description

The numberic keypad on your mobile phone looks like below:

1 2 5 8 9  0

Suppose you is holding your mobile phone with a single hand. Your thumb points at digit 1. Each time you can 1) press the digit your thumb pointing on, 2) Move your thumb right, 3) move your thumb down. Moving your thumb left or up are not allowed.

By using the numeric keypad under above constrains, can produce some numbers like 177 or 480 while producing other num Bers like 590 or are impossible.

Given A number K, find out the "maximum number less than" or equal to K "can be produced.

Input

The first line contains an integer T, the number of testcases.

Each testcase occupies a, a single, and an integer K.

For 50% of the data, 1 <= K <= 999.

For 100% of the data, 1 <= K <= 10^500, T <= 20.

Output

For each testcase output one line, the maximum number less than or equal to the corresponding K so can be produced.

Sample Input
32583131
Sample Output
2580129

Solution:
1#include <iostream>2#include <vector>3 using namespacestd;4 5 intm[Ten][Ten] = {{1,0,0,0,0,0,0,0,0,0},6{1,1,1,1,1,1,1,1,1,1},7{1,0,1,1,0,1,1,0,1,1},8{0,0,0,1,0,0,1,0,0,1},9{1,0,0,0,1,1,1,1,1,1},Ten{1,0,0,0,0,1,1,0,1,1}, One{0,0,0,0,0,0,1,0,0,1}, A{1,0,0,0,0,0,0,1,1,1}, -{1,0,0,0,0,0,0,0,1,1}, -{0,0,0,0,0,0,0,0,0,1}}; the  -  - intMain () { -Ios_base::sync_with_stdio (false); +     intT; -CIN >>T; +      while(t--) { Avector<Char>_s; at         stringstr; -CIN >>str; -         CharCH = str[0]; - _s.push_back (CH); -         inti; -         BOOLF =true; in          for(i =1; F && i < str.length (); ++i) { -             if(m[ch-'0'][str[i]-'0']) { toCH =Str[i]; + _s.push_back (CH); -             } the             Else { *                 CharCC =Str[i]; $                  while(1) {Panax Notoginseng                     Charc =_s.back (); -                     intK = CC-'0'-1; the                      for(; k >=0; --k) { +                         if(m[c-'0'][K]) Break; A                     } the  +                     if(k >=0) { -                          for(intj =0; J < I; ++j) { $cout <<_s[j]; $                         } -cout <<K; -CC ='0'+K; the                          for(intj = i +1; J < Str.length (); ++j) { -                             ints =9;Wuyi                              for(; s >=0; --s) { the                                 if(m[cc-'0'][s]) Break; -                             } WuCC ='0'+s; -cout <<cc; About                         } $F =false; -                          Break; -                     } -                     Else { ACC =C; + _s.pop_back (); thei--; -                         if(_s.empty ()) { $cc--; the                             if(CC = ='0') { the                                  for(intj =1; J < Str.length (); ++j) { thecout <<'9'; the                                 } -                             } in                             Else { thecout <<cc; the  About                                  for(intj =1; J < Str.length (); ++j) { the                                     ints =9; the                                      for(; s >=0; --s) { the                                         if(m[cc-'0'][s]) Break; +                                     } -CC ='0'+s; thecout <<cc;Bayi                                 } the                             } theF =false; -                              Break; -                         } the                     } the                 } the             } the         } -         if(F) { the              for(inti =0; I < _s.size (); ++i) cout <<_s[i]; the         } thecout <<Endl;94     } the}

Hihocoder #1153 Numeric Keypad

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.