解题报告:
这道题可以比较直白的办法,就是建立一个真假表格,验证是不是每一位都符合条件,一旦不符合,表格的这个位置为假,最后扫一遍表格,找出那些可以就行啦。
AC代码:
/* ID: yuanmz91 PROG: namenum LANG: C++ */ #include <fstream> #include <string.h> #include <memory.h> using namespace std; const int DICT_LEN = 5000; const int NAME_LEN = 15; bool compare(char, char); int main() { ifstream fin("namenum.in"); ifstream dictin("dict.txt"); ofstream fout("namenum.out"); int mark = 0, len; char dictionary[DICT_LEN][NAME_LEN]; char cowname[NAME_LEN]; bool finder[DICT_LEN], flag = false; memset(finder, true, sizeof(finder)); while(dictin >> dictionary[mark++]); fin >> cowname; len = strlen(cowname); for(int i = 0; i != mark; ++i) { if(strlen(dictionary[i]) != len) { finder[i] = false; } } for(int i = 0; i != len; ++i) { for(int j = 0; j != mark; ++j) { if(!compare(cowname[i], dictionary[j][i])) { finder[j] = false; } } } for(int i = 0; i != mark; ++i) { if(finder[i]) { fout << dictionary[i] << endl; flag = true; } } if(!flag) { fout << "NONE" << endl; } return 0; } bool compare(char x, char y) { switch(x) { case '2': if((y == 'A') || (y == 'B') || (y == 'C')) { return true; } break; case '3': if((y == 'D') || (y == 'E') || (y == 'F')) { return true; } break; case '4': if((y == 'G') || (y == 'H') || (y == 'I')) { return true; } break; case '5': if((y == 'J') || (y == 'K') || (y == 'L')) { return true; } break; case '6': if((y == 'M') || (y == 'N') || (y == 'O')) { return true; } break; case '7': if((y == 'P') || (y == 'S') || (y == 'R')) { return true; } break; case '8': if((y == 'V') || (y == 'T') || (y == 'U')) { return true; } break; case '9': if((y == 'W') || (y == 'X') || (y == 'Y')) { return true; } break; } return false; }
英文版:
Name That NumberAmong the large Wisconsin cattle ranchers, it is customary to brand cows with serial numbers to please the Accounting Department. The cow hands don't appreciate the advantage of this filing system, though, and wish to call the members of their herd by a pleasing name rather than saying, "C'mon, #4734, get along."
Help the poor cowhands out by writing a program that will translate the brand serial number of a cow into possible names uniquely associated with that serial number. Since the cow hands all have cellular saddle phones these days, use the standard Touch-Tone(R) telephone keypad mapping to get from numbers to letters (except for "Q" and "Z"):
2: A,B,C 5: J,K,L 8: T,U,V 3: D,E,F 6: M,N,O 9: W,X,Y 4: G,H,I 7: P,R,S
Acceptable names for cattle are provided to you in a file named "dict.txt", which contains a list of fewer than 5,000 acceptable cattle names (all letters capitalized). Take a cow's brand number and report which of all the possible words to which that number maps are in the given dictionary which is supplied as dict.txt in the grading environment (and is sorted into ascending order).
For instance, the brand number 4734 produces all the following names:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
As it happens, the only one of these 81 names that is in the list of valid names is "GREG".
Write a program that is given the brand number of a cow and prints all the valid names that can be generated from that brand number or ``NONE'' if there are no valid names. Serial numbers can be as many as a dozen digits long.
4734
GREG中文版:
下面是测试数据:
Here are the test data inputs: ------- test 1 ---- 4734 ------- test 2 ---- 234643 ------- test 3 ---- 5747867437 ------- test 4 ---- 223 ------- test 5 ---- 532 ------- test 6 ---- 546 ------- test 7 ---- 53662 ------- test 8 ---- 5455426 ------- test 9 ---- 26678268463 ------- test 10 ---- 463373633623 ------- test 11 ---- 282742662 ------- test 12 ---- 463373633623 ------- test 13 ---- 2336 ------- test 14 ---- 5264 ------- test 15 ---- 426