Name That Number

来源:互联网 发布:台湾为什么不独立知乎 编辑:程序博客网 时间:2024/05/19 22:59


Name That Number

Among 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.


PROGRAM NAME: namenum



INPUT FORMAT



A single line with a number from 1 through 12 digits in length.

SAMPLE INPUT (file namenum.in)



4734

OUTPUT FORMAT



A list of valid names that can be generated from the input, one per line, in ascending alphabetical order.

SAMPLE OUTPUT (file namenum.out)



GREG



/*ID: des_jas1PROG: namenumLANG: C++*/#include <iostream>#include <fstream>#include <string>//#define fin cin//#define fout coutusing namespace std;string ss,cc;char dic[3];bool Is_Accord(char a,int j){switch(ss[j]) //和ss比较{case '2':dic[0]='A';dic[1]='B';dic[2]='C';break;case '3':dic[0]='D';dic[1]='E';dic[2]='F';break;case '4':dic[0]='G';dic[1]='H';dic[2]='I';break;case '5':dic[0]='J';dic[1]='K';dic[2]='L';break;case '6':dic[0]='M';dic[1]='N';dic[2]='O';break;case '7':dic[0]='P';dic[1]='R';dic[2]='S';break;case '8':dic[0]='T';dic[1]='U';dic[2]='V';break;case '9':dic[0]='W';dic[1]='X';dic[2]='Y';break;default:break;}int i;for(i=0;i<=3;i++)if(a==dic[i])//满足一种翻译就可以了return true;return false;}int main() {ofstream fout ("namenum.out");    ifstream fin ("namenum.in");ifstream dic ("dict.txt");fin>>ss;string::size_type len=ss.size(),ll;int flag2=1;while(dic>>cc)//从dict文档里面一个一个调出来{ll=cc.size();if(ll==len) //先判断长度是否相等{int i,flag=1;for(i=0;i<ll;i++){if(!Is_Accord(cc[i],i)){flag=0; //有一个不一样就退出循环break;}}if(flag)//正常退出即符合条件可以输出{fout<<cc<<endl;flag2=0;   //是否有输出的标记}}}if(flag2)fout<<"NONE"<<endl;fout.close();fin.close();dic.close();    return 0;}




原创粉丝点击