HDU1274

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1274
参考博客:https://mp.weixin.qq.com/s?__biz=MzUzMjU2NjYxMA==&mid=2247483728&idx=1&sn=85f272a7a184168595365c409832b90d&scene=21#wechat_redirect

这个题是递归,但是有很多情况要考虑,且递归函数不仅仅有一个作用,有输出字符串和查找对应括号位置的功能,

#include <string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>

using namespace std;

char * print(char * s)
{
    char *t = s;
    if(*t == 0||*t == ')') return t;
    for(; *t; t++) {
        if(*t == ')') return t;
        if(isalpha(*t)) {
            putchar(*t); continue;
        }
        int D = 0;
        while(isdigit(*t)) {
            D = D*10+*t-'0';t++;
        }
        D = D?D:1;
        if(*t == '(') {
            char * n = t;
            for(int i = 0; i < D; ++i)
                n = print(t+1);
            t = n;
         }
         else {
            for(int i = 0; i < D; ++i){
                putchar(*t);
            }
        }
    }

}
int main()
{
    char s[1007];
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", s);
        print(s);
        print("\n");

    }
    return 0;
}
发布了21 篇原创文章 · 获赞 12 · 访问量 727

猜你喜欢

转载自blog.csdn.net/weixin_44070289/article/details/104070050