背景:
今天无意间在VJ-Status看到有人提交UVA的题目,还好是水题一枚,哈哈。
题意:
模拟手机九宫格输入法,把字母输入转换成数字输出。
输入类似“1-HOME-SWEET-HOME”。
输出类似“1-4663-79338-4663”。
分析:
其实是水题,完全不用分析,可本着"书写重新思考"的宗旨,再简单的东西也得认真把思路写一写!
因为完全是字符处理,放弃字符串方式。 → 直接逐个字符输入进行处理。
按照字符大小进行对应输出,因为没用else,所以得特别注意各if之间不会有重叠区间。
#include <iostream>
#include <stdio.h>
using namespace std;
//#define LOCAL_TEST
int main()
{
#ifdef LOCAL_TEST
freopen("..\\in.txt", "r", stdin);
freopen("..\\out.txt", "w+", stdout);
#endif
// 尝试别用F盘路径了。用相对路径
char ch;
// while ( cin >>ch ) // error, unable to get special character like 'enter'
while ( (ch=cin.get()) != EOF )
{
if ( ch >= 'A' && ch <= 'O' )
cout << (ch-'A')/3+2;
if ( ch >='P' && ch<='S' )
cout <<'7';
if ( ch >='T' && ch<='V' )
cout <<'8';
if ( ch >='W' && ch<='Z' )
cout <<'9';
if ( ch<'A' || ch>'Z' )
cout <<ch;
} // end while
return 0;
}