zoukankan      html  css  js  c++  java
  • nyoj1272表达式求值(递归法)

    递归写的,类似于之前的一道,但要更难一点,因为加入了'+','*'以及括号运算符,所以要考录周全;

    这道题给了我很大启示,在第一道德基础上;

    1!寻找括号的优先级,先找有没有不被任何括号夹住的加号,如果有将其两端递归,否则(此时说明没有空出来的加号)继续找有没有没被括号夹住的

    ‘×’号,如果有将乘号两端继续递归

    #include<iostream>
    #include<cstring>
    using namespace std;
    #define LL long long
    char s[1010];
    LL judnum(int,int);
    int judge(int ,int ,char);
    LL all(LL);
    LL dfs(int st,int ed)
    {
    int i,j,pp=judge(st,ed,'+'),pnum=judnum(st,ed),pc=judge(st,ed,'*');
       if(pnum!=-1)        return pnum;
       else if(pp!=-1) {LL t1=dfs(st,pp-1),t2=dfs(pp+1,ed);
                            return t1+t2;}

        else if (pc!=-1){
        LL t1=dfs(st,pc-1),t2=dfs(pc+1,ed);
        return t1*t2;
    }
    else if(s[st]=='('&&s[ed]==')') return dfs(st+1,ed-1);
    else if(s[st]=='S'){
    int mid=judge(st,ed,',');
    LL t1=dfs(st+5,mid-1),t2=dfs(mid+1,ed-1);
    return max(all(t1),all(t2));
      }
    }
    LL all(LL a)
    {
    LL s=0;
    while(a){
    s+=a%10;
    a/=10;}
    return s;
    }
    LL judnum(int st,int ed)
    {
    int sum=0;
    for(int i=st;i<=ed;++i)
    {
    if(!isdigit(s[i])) return -1;
    else sum=sum*10+s[i]-'0';
    }
    return sum;
    }
    int judge(int st,int ed,char type)
    {
        int c=0,pd=(type==','?1:0);
        for(int i=st;i<=ed;++i){
            if(s[i]=='(') ++c;
            else if(s[i]==')') --c;
            if(c==pd&&s[i]==type) return i;
        }
        return -1;
    }
    int main()
    {
    int t;
    cin>>t;
    while(t--) cin>>s,cout<<dfs(0,strlen(s)-1)<<endl;
    return 0;
    }

    上面那个太傻逼了= =

     #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    char str[1005];
    int Jud(int l, int r);
    int Change(int t);
    int main(void)
    {
    int T, len;
    scanf("%d", &T);
    while(T--)
    {
    scanf("%s", str+1);
    len = strlen(str+1);
    printf("%d ", Jud(1, len));
    }
    return 0;
    }

    int Jud(int l, int r)
    {
    int i, k;
    k = 1;
    for(i=l;i<=r;i++)
    {
    if(str[i]=='(')
    k++;
    if(str[i]==')')
    k--;
    if(str[i]=='+' && k==1)
    return Jud(l, i-1)+Jud(i+1, r);
    }
    k = 1;
    for(i=l;i<=r;i++)
    {
    if(str[i]=='(')
    k++;
    if(str[i]==')')
    k--;
    if(str[i]=='*' && k==1)
    return Jud(l, i-1)*Jud(i+1, r);
    }
    if(str[l]=='(')
    return Jud(l+1, r-1);
    if(str[l]>='0' && str[l]<='9')
    {
    sscanf(str+l, "%d", &k);
    return k;
    }
    k = 1;
    for(i=l+5;i<=r-1;i++)
    {
    if(str[i]=='(')
    k++;
    if(str[i]==')')
    k--;
    if(str[i]==',' && k==1)
    return max(Change(Jud(l+5, i-1)), Change(Jud(i+1, r-1)));
    }
    }

    int Change(int t)
    {
    int sum;
    sum = 0;
    while(t!=0)
    {
    sum += t%10;
    t /= 10;
    }
    return sum;
    }
  • 相关阅读:
    Win10下TensorFlow安装错误解决:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
    OA Framework or ADF?
    Why do I get the error REP-0713 when I run my report?
    Example Report Using FND FORMAT_CURRENCY
    FND SRWINIT & FND SRWEXIT
    【转】Oralce PL/SQL 堆栈信息追踪
    .net core 杂记:用Autofac替换内置容器
    .net core 杂记:日记记录
    Entity Framework 查漏补缺 (三)
    Entity Framework 查漏补缺 (二)
  • 原文地址:https://www.cnblogs.com/zzqc/p/6635899.html
Copyright © 2011-2022 走看看