Name That Number(模拟)

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

   题意:

   有一串数字,用上面的”字母数字对应表“规则来找出所有“dict.txt”中的名字。

   思路:

   先将dict.txt中全部的名字放在namelist[ i ][ j ]中,然后输入一串数字(这串中的数字由一个个字符组成,不是数),当这个名字长度与这串数字字符串的长度相同时,再一个个进行比较,如果全都满足条件,则输出该字符,如果不符合则判断下一个,如果遍历完全部名字后也找不到一个,则输出NONE。

   AC:

 

/*
TASK:namenum
LANG:C
ID:sum-g1
*/
#include<stdio.h>
#include<string.h>

int translate(char a,char b)   //另外写个函数判断是否满足条件
{
 if((a=='A'||a=='B'||a=='C')&&b=='2') return 1;
 if((a=='D'||a=='E'||a=='F')&&b=='3') return 1;
 if((a=='G'||a=='H'||a=='I')&&b=='4') return 1;
 if((a=='J'||a=='K'||a=='L')&&b=='5') return 1;
 if((a=='M'||a=='N'||a=='O')&&b=='6') return 1;
 if((a=='P'||a=='R'||a=='S')&&b=='7') return 1;
 if((a=='T'||a=='U'||a=='V')&&b=='8') return 1;
 if((a=='W'||a=='X'||a=='Y')&&b=='9') return 1;
 return 0;
}

int main()
{
	FILE *list;
 FILE *fin =fopen("namenum.in","r");
 FILE *fout=fopen("namenum.out","w");
	char namelist[5000][15];
	int i=0,sum,j,sum1;
	int temp=0,numlen,namelen;
	char num[15],num1[15];
	
	list=fopen("dict.txt","r"); //文件中的名字复制过程
	while(!feof(list))          //判断文件读取完没有,如果读取完则返回1
	{fscanf(list,"%s",namelist[i++]);}
	
    sum=i;
    fscanf(fin,"%s",num);   //输入一串字符串数字
    numlen=strlen(num);
    for(i=0;i<sum;i++)      //循环名字表
    {
    	namelen=strlen(namelist[i]);
    	if(namelen==numlen)   //判断字符串大小
    	{
    		sum1=0;
		for(j=0;namelist[i][j];j++)  //循环这个名字
    		{
                 if(translate(namelist[i][j],num[j]))sum1++;	 
		}
    		if(sum1==namelen)
    		{
    			fprintf(fout,"%s\n",namelist[i]);
    			temp=1;
    		}
    	}
    }
    if(!temp) fprintf(fout,"NONE\n");
    exit(0);
}

   总结:

   题目很简单,我也不懂自己为什么可以磨这么久才磨出来……一开始把输入的1当成数字1而不是字符‘1’,然后处理文件部分又不怎么会处理,犯的低级错误太多了。题目一下子就懂了,但是懂了不代表你马上就能准确的写出来,防止眼高手低,所以无论是简单还是难,都必须逼自己写一遍。简单的都不会,都不熟悉,都那么粗心的话,那难的就更难做AC了。So,继续努力吧。

你可能感兴趣的:(number)