Topic links: Good Morning
Title effect: pressing numbers on the keypad, only on the basis of this position by the right down, and outputs the minimum absolute values of difference to the requirements
AC code is as follows:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 using namespace std; 6 const int maxn=1e9+10; 7 int a[10][10]; 8 bool vis[210]; 9 int f[210]; 10 int k; 11 12 bool judge(int x) 13 { 14 int TOT = 0 ; 15 int S [ 10 ]; 16 for (; X; X / = 10 ) . 17 { 18 is S [TOT ++] = X% 10 ; . 19 } 20 is for ( int I = TOT; I> . 1 ; i--) // determines whether the subject every given output condition 21 is { 22 is iF (a [S [I]] [S [I-! . 1 ]]) return to false ; 23 is } 24 return to true ; 25 } 26 27 void the init () 28 { 29 // play table 1.A [i] [j] denotes the i key to play key table 1 j, i from the other not to bond to 0 30 A [ 1 ] [ 1 ] = . 1 ; A [ . 1 ] [ 2 ] = . 1 ; A [ . 1 ] [ . 3 ] = . 1 ; A [ . 1 ] [ . 4 ] = . 1 ; A [ . 1 ] [ . 5 ] = . 1 ; A [ . 1 ] [ . 6 ] = . 1 ; A [ . 1 ] [ . 7 ] = . 1 ; A [ . 1 ] [8]=1; a[1][9]=1; a[1][0]=1; 31 a[2][2]=1; a[2][3]=1; a[2][5]=1; a[2][6]=1; a[2][8]=1; a[2][9]=1; a[2][0]=1; 32 a[3][3]=1; a[3][6]=1; a[3][9]=1; 33 a[4][4]=1; a[4][5]=1; a[4][6]=1; a[4][7]=1; a[4][8]=1; a[4][9]=1; a[4][0]=1; 34 a[5][5]=1; a[5][6]=1; a[5][8]=1; a[5][9]=1; a[5][0]=1; 35 a[6][6]=1; a[6][9]=1; 36 a[7][7]=1; a[7][8]=1; a[7][9]=1; a[7][0]=1; 37 a[8][8]=1; a[8][9]=1; a[8][0]=1; 38 a[9][9]=1; 39 a[0][0 ] = . 1 ; 40 41 is Memset (VIS, to false , the sizeof (VIS)); 42 is Memset (F, - . 1 , the sizeof (F)); 43 is // play table 2, all the numerals coincidence output condition between 0-210 the number 44 is for ( int i = 0 ; i <= 210 ; i ++ ) 45 { 46 is IF (Judge (i)) VIS [i] = to true ; 47 } 48 // play table 3, violence test i line with output number of test. 1-I, I-2, I-2,1,0 ,,,. 3; I; I +. 1, I + 2,. 3 I +, I + 210 .... 49 for ( int i=1; i<=200; i++ ) 50 { 51 for(int j=0; j<=210; j++ ) 52 { 53 if( i-j>=0 && vis[i-j] ) 54 { 55 f[i]=i-j; 56 break; 57 } 58 if( i+j<=210 && vis[i+j] ) 59 { 60 f[i]=i+j; 61 break; 62 } 63 } 64 } 65 } 66 67 int main() 68 { 69 int t; 70 scanf("%d",&t); 71 init(); 72 while( t-- ) 73 { 74 scanf("%d",&k); 75 printf("%d\n",f[k]); 76 } 77 Return 0 ; 78 }