#UVa:10921-Find the Telephone

灆洢 2015-01-03 22:21:17

利用對照表對映得解即可。

C++(0.022)

/*******************************************************/
/* UVa 10921 Find the Telephone                        */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2015/01/03                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
using namespace std;

int main(){
  const int table[30] = { 2, 2, 2,
                          3, 3, 3,
                          4, 4, 4,
                          5, 5, 5,
                          6, 6, 6,
                          7, 7, 7, 7,
                          8, 8, 8,
                          9, 9, 9, 9 };
  string input;
  while( getline( cin, input ) ){
    for( int i = 0 ; i < input.length() ; ++i ){
      if( isdigit(input[i] ) || input[i] == '-' ){
        printf("%c", input[i]);
      }
      else{
        printf("%d", table[(int)(input[i]-'A')]);
      }
    }
    printf("\n");
  }

  return 0;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料