现在的位置: 首页 > 综合 > 正文

UVa 10260 – Soundex

2013年12月05日 ⁄ 综合 ⁄ 共 535字 ⁄ 字号 评论关闭

题目:编码翻译,有些字母有对应的数字,有的没有,如果连续对应的数字相同只输出一个。

分析:简单题。直接转化即可。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int rep( char c )
{
	switch( c ) {
		case 'B':
		case 'F':
		case 'P':
		case 'V': return 1;
		case 'C':
		case 'G':
		case 'J':
		case 'K':
		case 'Q':
		case 'S':
		case 'X':
		case 'Z': return 2;
		case 'D':
		case 'T': return 3;
		case 'L': return 4;
		case 'M':
		case 'N': return 5;
		case 'R': return 6;
		default : return 0;
	}
}

int main()
{
	char temp[25];
	while ( cin >> temp ) {
		for ( int i = 0 ; temp[i] ; ++ i ) {
			if ( i > 0 && rep(temp[i]) == rep(temp[i-1]) )
				continue;
			if ( !rep(temp[i]) ) continue;
			cout << rep(temp[i]);
		}
		cout << endl;
	}
	return 0;
}

抱歉!评论已关闭.