輸入一個單字,把它編碼成對應的數字
1 represents B, F, P, or V
2 represents C, G, J, K, Q, S, X, or Z
3 represents D or T
4 represents L
5 represents M or N
6 represents R
A, E, I, O, U, H, W, and Y are not represented in Soundex coding,
具有相同編碼字母,如果相鄰或重複出現,僅要輸出一個數字
用if判斷式把編碼寫入變數b,如果不是 Soundex coding裡的字母,就讓b為0。
而變數a是為了讓字母重複出現時不要輸出,故當b!=a && b!=0才會輸出編碼數字
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
while(cin>>s){
int a=0,b;
for(int i=0;i<s.size();i++){
if(s[i]=='C'||s[i]=='G'||s[i]=='J'||s[i]=='K'||s[i]=='Q'||s[i]=='S'||s[i]=='X'||s[i]=='Z') b=2;
else if(s[i]=='B'||s[i]=='F'||s[i]=='P'||s[i]=='V') b=1;
else if(s[i]=='D'||s[i]=='T') b=3;
else if(s[i]=='L') b=4;
else if(s[i]=='M'||s[i]=='N') b=5;
else if(s[i]=='R') b=6;
else b=0;
if(b!=a && b!=0) cout<<b;
a=b;
}
cout<<endl;
}
}
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main() {
int n;
string str;
cin >> n;
while (n > 0) {
cin >> str;
if (str.length()==3) {//依題目單字長度一定是正確的,所以one two必為3
//依題目所講至多一個錯誤的字母,所以可能on?, ?ne, o?e 三種組合,啊都不是的話就是two
if ((str[0]=='o'&&str[1]=='n') || (str[1]=='n'&&str[2]=='e') || (str[2]=='e'&&str[0]=='o'))
cout << "1" << endl;
else
cout << "2" << endl;
}
else cout << "3" << endl;
n--;
}
}