Basic
memory limit: 65536KB time limit: 500MS
Description
The programming language Ada has integer constants that look like this: 123, 8#123#, 16#abc#. These constants represent the integers 123, 83 (123 base 8) and 2748 (abc base 16). More precisely, an integer may be a decimal integer given as a sequence of one or more digits less than 10, or it may be an integer to some specific base, given as the base followed by a sequence of one or more digits less than the base enclosed by # symbols. Lower case letters from a through f are used as the digits representing 10 through 15. In Ada, the base, if specified, must be a sequence of decimal digits. For this problem, however, the base may be of any form described above so long as it represents an integer between 2 and 16 inclusive.
Input
The first line of input contains a positive integer n. n lines follow.Input lines contain no spaces and are between 1 and 80 characters in length.
Output
For each line of input, output a line "yes" if it is a valid integer constant according to the above rules; otherwise output a line containing "no".
Sample Input
5
2#101#
2#101##123#
17#abc#
16#123456789abcdef#
16#123456789abcdef#123456789abcdef#
Sample Output
yes
yes
no
yes
no
Source
ZQUCPC个人赛4
Author
Waterloo local
题意:
给一串字符串,判断是否合法。合法情况为:第一个数字在2至16间,表示进制的基底,然后是一个用两个#包含在内的数字,表示该进制下的数字,用a到f表示10到15,新算出的值可以作为下一个数的基底(如果后面还有数的话)。
提交次数:
9次,其中8次WA, 1次AC
问题的分析与解题思路:
这个题其实不难,挺水的,用递归可以解决。只是一些边缘问题要考虑,我就是因为这个贡献了8个WA,主要还是粗心大意啊。
说一下注意的地方:
1. 注意精度问题,因为字符可以很长的,算出的数可能会超出整型范围,明显整型不能保存。
2. 给一串0是合法的,少#多#是不合法的,基底不能超出2到16的范围。
3. 某基底下的数的每一位都不能超出那个基底允许的范围。
4. 写递归函数时要将基底为10以上的分开来讨论。
AC方法和数据结构:
把字符串作为全局变量,构造一个递归函数,传入的值有两个,一个是基底,另一个是开始的‘#’后一维的位置。该递归函数返回1或者0,表示当前一步是否合法。主函数调用递归函数前先算出第一次的基底。
AC代码:
PS: 粗心你不得好死。。。。。