各路大神求用栈做的算法(标程也行)

好像能用**双栈**做,但是我不会……求各路大神指教。

3 条评论

  • @ 2016-11-12 17:29:50

    不要用双栈,直接把乘号连接的数看成一个整体。
    标程如下:
    pascal
    var
    i,j,k,num,ans,x,num2:longint;
    s:ansistring;
    begin
    readln(s);
    k:=1;
    ans:=0;
    while k<=length(s) do
    begin
    num:=0;
    while (s[k]>='0')and(s[k]<='9') do
    begin
    num:=(num*10+ord(s[k])-48)mod 10000;
    inc(k);
    end;
    while s[k]='*' do
    begin
    num2:=0;
    inc(k);
    while (s[k]>='0')and(s[k]<='9') do
    begin
    num2:=(num2*10+ord(s[k])-48)mod 10000;
    inc(k);
    end;
    num:=(num*num2)mod 10000;
    end;
    ans:=(ans+num)mod 10000;
    inc(k);
    end;
    writeln(ans);
    end.

  • @ 2016-10-22 09:33:16
    #include<cstdio>
    #include<cstring>
    #define mod 10000
    #define maxn (1000000 + 10)
    #define f(x,y) for( register int x = 1;x <= y;x ++)
    using namespace std;
    char c[maxn];
    int st[maxn];
    int pos = 0;
    #define top st[pos]
    #define pop1() pos--
    int main() {
        scanf("%s",c + 1);
        int len = strlen(c + 1);
        int temp = 0;
        f(i,len+1) { 
            if (c[i] >= '0' && c[i] <= '9') temp = (temp * 10 + c[i] - '0') % mod;
            else {
                if (top == -2) {
                    pop1();
                    int t = (top * temp) % mod;
                    st[pos] = t;
                }else st[++ pos] = temp;
                if(c[i] == '*') st[++ pos] = -2;
                temp = 0;
            }
        }
        int ans = 0;
        pos ++;
        while(pos --) ans = (ans + st[pos]) % mod;
        printf ("%d",ans);
        return 0;
    }
    
  • @ 2016-10-22 09:32:32

    #include<cstdio>
    #include<cstring>
    #define mod 10000
    #define maxn (1000000 + 10)
    #define f(x,y) for( register int x = 1;x <= y;x ++)
    using namespace std;
    char c[maxn];
    int st[maxn];
    int pos = 0;
    #define top st[pos]
    #define pop1() pos--
    int main() {
    scanf("%s",c + 1);
    int len = strlen(c + 1);
    int temp = 0;
    f(i,len+1) {
    if (c[i] >= '0' && c[i] <= '9') temp = (temp * 10 + c[i] - '0') % mod;
    else {
    if (top == -2) {
    pop1();
    int t = (top * temp) % mod;
    st[pos] = t;
    }else st[++ pos] = temp;
    if(c[i] == '*') st[++ pos] = -2;
    temp = 0;
    }
    }
    int ans = 0;
    pos ++;
    while(pos --) ans = (ans + st[pos]) % mod;
    printf ("%d",ans);
    return 0;
    }

  • 1

信息

ID
1849
难度
7
分类
(无)
标签
递交数
3446
已通过
731
通过率
21%
被复制
2
上传者