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,继续努力吧。
相关推荐
withdrawal function that returns a bool value deposit function that returns a bool value a way to get available funds should return a double a way to create an account that returns bool value ...
【七年级上册英语全册复习教案】 在学习七年级上册英语时,学生需要掌握一系列基础知识点,以便为后续的英语学习打下坚实的基础。...教师在教学过程中应注重实践和情境模拟,帮助学生熟练运用所学知识。
1. `<property><name>fs.checkpoint.period</name><value>3600</value><description>The number of seconds between two periodic checkpoints.</description></property>`:该属性指定 namenode 每隔多长时间记录一...
接着,1.2节重点是完整搜索,如"Milking Cows"中运用离散化技术,"Transformations"和"Name That Number"通过枚举解决,而"Palindromic Squares"和"Dual Palindromes"进一步强化了枚举法的应用。 1.3节围绕贪心算法...
通过举例和练习题帮助学生理解并应用这些冠词,如:“This is a ruler and that is a pen. The ruler is green and the pen is black.”、“There is a house in the picture. There is an old woman near the house...
此外,“Transformations”、“Name That Number”等题目进一步加深了对模拟算法的理解。 ### 四、Chapter3:Techniques more subtle 第三章深入探讨了更精细的技术,如“Mixing Milk”题目虽然可能通过模拟解决,...
在学习这些知识点的同时,要注重实际应用,通过练习对话和情景模拟来加深理解,提高英语交际能力。记住,反复练习是掌握新知识的关键,所以不要怕犯错误,勇敢地说出来,你的英语水平将会逐步提高。在日常生活中,尽...
print("It's not that cold.") } ``` - **for 循环**:遍历序列或集合中的元素,如: ```swift for i in 1...5 { print(i) } ``` - **while 循环**:当满足某个条件时重复执行一段代码,如: ```swift ...
上课前,老师可能会说“It's time for class.”和“Open your books and turn to page [number]”。点名时,学生应答“Here!”,确认材料齐全可问“Has everybody got a sheet?”。讲解内容时,老师可能询问学生是否...
),Girl 回答后,Boy 表达感谢(Thank you),Girl 回应不客气(You’re welcome/That’s OK)。 接着,Bob 和 Alice 的对话同样涵盖这些基础内容。Bob 问候 Alice(Good morning, Alice!),Alice 回应(Good ...
#### Name That Number 此题要求根据给定的条件猜出一个数字。这类问题常常涉及数学推理和逻辑分析,可以通过枚举法、二分查找等算法来逐步缩小可能的数字范围,直至找到正确的答案。 #### Palindromic Squares ...
That's my first name."时,这表明她希望你用她的名字Mary来称呼她,所以合适的提问应是"How shall I address you?"。同样,询问电话号码可以用"could you please tell me what's the telephone number on my desk?...
**1.2.3 Name That Number** - **问题描述**:题目要求找出一个数的某种特殊性质。 - **算法思想**:此题可能涉及到数学知识的应用,如数论中的质数判断等。 **1.2.4 Palindromic Squares** - **问题描述**:寻找...
通过给Number.prototype添加方法,我们可以模拟一个整数类型的方法。 **示例代码:** ```javascript Number.method('integer', function() { return Math[this ](this); }); ``` 通过这段代码,我们给Number....
I’d like to check out now please.",酒店退房时,服务员可能会询问"Your name and room number, please?",确认信息,选项C正确。 - "May I take your order, sir?",在餐厅点餐时,可以回答自己想点的菜品。 ...
1526 Big Number 简单题,不过O(1)和O(n)还是有区别的:) 1529 Enigmatic Travel 简单题,不过个人感觉题目描述很令人费解 1530 Find The Multiple 简单题 1537 Playing with a Calculator 简单题 1577 GCD & ...
1526 Big Number 简单题,不过O(1)和O(n)还是有区别的:) 1529 Enigmatic Travel 简单题,不过个人感觉题目描述很令人费解 1530 Find The Multiple 简单题 1537 Playing with a Calculator 简单题 1577 GCD & ...
- **求职者回答**:“My number is sixteen and my name is Zhixin Zhang.”(我是16号,我叫张志新。) **小贴士**:面试官可能会提出一些看似普通的问题,但这些问题可能隐藏着更深层次的考察目的。例如,“How ...
6. 游戏This/That He/She is... 通过描述人物或物品,教师可以引导学生进行角色扮演,加深对职业、性别等词汇的记忆。 7. 游戏Bingo:一种常见的课堂游戏,学生需要在卡片上找出教师所读出的单词。这个游戏适用于...
Design contains <number> input pin(s) that do not drive logic **描述**: 设计中有一定数量的输入引脚没有驱动任何逻辑。 **原因**: 输入引脚未被正确连接至其他逻辑组件。 **解决方法**: 检查设计逻辑,确保...