AOJ - Problem 0127 : Pocket Pager Input
問題文の通りに文字列を変換します。入力した文字列の長さが奇数か変換表にないときは"NA"を出力します。
#include <iostream> #include <string> using namespace std; char table[6][5] = { {'a','b','c','d','e'}, {'f','g','h','i','j'}, {'k','l','m','n','o'}, {'p','q','r','s','t'}, {'u','v','w','x','y'}, {'z','.','?','!',' '} }; int main(){ string s; while( cin >> s ){ string ans; if( s.size()%2 ){ cout << "NA" << endl; }else{ bool flag = false; for(int i=1 ; i < s.size() ; i+=2 ){ int y = s[i-1]-'0'-1; int x = s[i]-'0'-1; if( y < 0 || x < 0 || y >= 6 || x >= 5 ){ flag = true; break; } ans += table[y][x]; } if( flag ) cout << "NA" << endl; else cout << ans << endl; } } }