poj 2338 Basic

Basic
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 880 Accepted: 219

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 2739 (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.
编程语言的Ada有整型常量,比如: 123 , 8 # 123 # , 16 # abc # 。这些常数代表123 , 83 ( 123基于8)和2739整数( abc基于16) 。更准确地说,一个整数可能是一个十进制整数,以一组序列组成,序列中每个数字小于10,或者它可以是一个整数,在一些特定的基础上,基础数列后面跟着用#符号括起来的以。。。为基础的数列。从a到f的小写字母被用作表示10至15的数字。在Ada,基础,如果指定,必须是十进制数字序列。对于这个问题,基础部分可以是所述的任何形式,只要其为2至16 (含)之间的整数。
(三个大字:没看懂)
别人的题目理解。。。反正我是看不懂了:
给一串字符串,判断是否合法。合法情况为:第一个数字在 2 16 间,表示进制的基底,然后是一个用两个 # 包含在内的数字,表示该进制下的数字,用 a f 表示 10 15 ,新算出的值可以作为下一个数的基底(如果后面还有数的话)[好不容易看懂题啊。。。。]

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. 
第一行输入包含多少组,之后n行,每行没空格,每行输入一个1-80个字符的字符串。

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".
对于每一行的输入,输出 "yes"如果这是有效正整数常量;否则输出"no"

Sample Input

5
2#101#
2#101##123#
17#abc#
16#123456789abcdef#
16#123456789abcdef#123456789abcdef#

Sample Output

yes
yes
no
yes
no

Source


普通递归一下就好,但是实在是情况太多了,于是一怒之下还是研究别人代码出成品,简直无奈。。。。。。
#include <iostream>
#include<algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;

char s[90];
int recu(int head, int i){
    int j;
    double g = 0;
    if (head < 2 || head > 16) return 0;

    if (s[i] == '#') return 0;
    for (j = i;s[j];++ j){
        if (s[j] == '#') break;
//        printf("c = %c\n", s[j]);
        if (head > 10 && ((s[j] < '0') || (s[j] > '9' && s[j] < 'a') || s[j] > 'a' + head - 11)) return 0;
        if (head <= 10 && !((s[j] >= '0') && (s[j] <= '0' + head - 1))) return 0;
//        printf("2 = %d, g = %d\n", j, g);
        if (s[j] >= 'a' && s[j] <= 'f') g = g*head + 10 + s[j] - 'a';
        else g = g * head + s[j] - '0';
//        printf("j = %d, g = %d\n", j, g);
    }
    if(s[j] == '#' && s[j+1] == 0 || s[j] == '#' && s[j+1] == '#' && recu(g, j+2))
        return 1;
    else return 0;
}
int main()
{
    int n, i, j, head;
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    scanf("%d", &n);
    for (i = 0;i < n;i ++){
        scanf("%s", s);
        head = 0;
        j = 0;
        for (j = 0;;++j){
            if (s[j] >= '0' && s[j] <= '9'){
                head = head * 10 + s[j] - '0';
//                printf("head:%d\n", head);
            }else break;
        }
//        printf("s[j] = %c\n", s[j]);
        if (s[j] == '#' && recu(head, j+1) || head == 0 && s[j] == 0) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值