九度OJ 1006:ZOJ问题 (递归)

来源:互联网 发布:linux重启服务器 编辑:程序博客网 时间:2024/06/05 15:04

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:18621

解决:3197

题目描述:
对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC。

是否AC的规则如下:
1. zoj能AC;
2. 若字符串形式为xzojx,则也能AC,其中x可以是N个'o' 或者为空;
3. 若azbjc 能AC,则azbojac也能AC,其中a,b,c为N个'o'或者为空;
输入:
输入包含多组测试用例,每行有一个只包含'z','o','j'三种字符的字符串,字符串长度小于等于1000。
输出:
对于给定的字符串,如果能AC则请输出字符串“Accepted”,否则请输出“Wrong Answer”。
样例输入:
zojozojoozoojoooozoojoooozoojozojooooozojozojoooo
样例输出:
AcceptedAcceptedAcceptedAcceptedAcceptedAcceptedWrong AnswerWrong Answer
来源:
2010年浙江大学计算机及软件工程研究生机试真题

思路:

从规则2 3来看,很明显需要用递归来解,不算难,但是边界条件判断容易出错。

我WA了几次才通过的。


代码:

#include <stdio.h> #define N 1000 int main(void){    int i, a, b, c;    char s[N+1];     while (scanf("%s", s) != EOF)    {        a = b = c = 0;        i = 0;        while (s[i] == 'o')        {            i ++;            a ++;        }        if (s[i] != 'z')        {            printf("Wrong Answer\n");            continue;        }        i++;        while (s[i] == 'o')        {            i ++;            b ++;        }        if (s[i] != 'j')        {            printf("Wrong Answer\n");            continue;        }        i++;        while (s[i] == 'o')        {            i ++;            c ++;        }        if (s[i] != '\0')        {            printf("Wrong Answer\n");            continue;        }         //if (c == a+a*(b-1) && b > 0)        while (b > 1)        {            b --;            c -= a;        }        if (a == c && a >= 0 && b == 1)            printf("Accepted\n");        else            printf("Wrong Answer\n");    }     return 0;}/**************************************************************    Problem: 1006    User: liangrx06    Language: C    Result: Accepted    Time:30 ms    Memory:912 kb****************************************************************/


0 0
原创粉丝点击