92pts,WA on #5,求调

P2830 写程序

Eason20120229 @ 2024-09-07 11:00:19

#include <iostream>
#include <map>
#include <string>
#define N    100
#define BASE 10

using namespace std;

struct arrn {
    int len;
    int rarr[N];
};

map< string, arrn > arrs;

string opt;
int cur;
bool flag;

bool isnum() {
    return opt[cur] >= '0' && opt[cur] <= '9';
}

string readstr() {
    string tmp;
    while (((opt[cur] >= 'a' && opt[cur] <= 'z') ||
            (opt[cur] >= 'A' && opt[cur] <= 'Z')) &&
           cur < opt.size()) {
        tmp += opt[cur];
        cur++;
    }
    return tmp;
}

int readnum() {
    int tmp = 0;
    while (isnum() && cur < opt.size()) {
        tmp = opt[cur] - '0' + tmp * BASE;
        cur++;
    }
    return tmp;
}

int getnum() {
    if (isnum()) {
        int num = readnum();
        return num;
    }
    string curname = readstr();
    cur++;
    int num = getnum();
    cur++;
    if (num >= arrs[curname].len) {
        return -1;
    }
    return arrs[curname].rarr[num];
}

int main() {
    while (getline(cin, opt)) {
        cur = 0;
        flag = false;
        if (opt[0] == 'i' && opt[1] == 'n' &&
            opt[2] == 't' && opt[3] == ' ') {
            cur = 4;
            string name;
            name = readstr();
            cur++;
            int num = getnum();
            if (num > N || num < 0) {
                cout << -1;
                return 0;
            }
            arrs[name].len = num;
        } else if (opt[0] == 'c' && opt[1] == 'o' &&
                   opt[2] == 'u' && opt[3] == 't' &&
                   opt[4] == ' ') {
            cur = 4 + 1;
            int num = getnum();
            if (num < 0) {
                cout << -1;
                return 0;
            }
            cout << num << endl;
        } else {
            string name;
            name = readstr();
            cur++;
            int num = getnum();
            if (num >= arrs[name].len || num < 0) {
                cout << -1;
                return 0;
            }
            cur++;
            cur++;
            int tmp = getnum();
            if (tmp < 0) {
                cout << -1;
                return 0;
            }
            arrs[name].rarr[num] = tmp;
        }
    }
    return 0;
}

by Eason20120229 @ 2024-09-16 20:37:52

@WHM_Funina


by WHM_Funina @ 2024-11-05 21:14:46

#include<bits/stdc++.h>
using namespace std;
struct Zu
{
    vector<int>v;
    Zu(int n)
    {
        v = vector<int>(n, 0);
    }
};
map<string, Zu*>mo;
int cha(string s)
{
    if ('0' <= s[0] && s[0] <= '9')
    {
        stringstream sin(s);
        int a;
        sin >> a;
        return a;
    }
    else
    {
        string zu, biao;
        while (zu.size() < s.size() && s[zu.size()] != '[')zu += s[zu.size()];
        biao = s.substr(zu.size() + 1, s.size() - zu.size() - 2);
        int a = cha(biao);
        if (a != -1)
        {
            Zu mu = *mo[zu];
            if (mu.v.size() <= a)
                return -1;
            else
                return mu.v[a];
        }
        else
            return -1;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    string s1, s2;
    while (cin >> s1 >> s2)
    {
        if (s1 == "int")
        {
            string zu, biao;
            while (zu.size() < s2.size() && s2[zu.size()] != '[')zu += s2[zu.size()];
            biao = s2.substr(zu.size() + 1, s2.size() - zu.size() - 2);
            int a = cha(biao);
            if (a == -1)
            {
                cout << -1;
                break;
            }
            else
            {
                Zu* t = new Zu(cha(biao));
                mo[zu] = t;
            }
        }
        else if (s1 == "cout")
        {
            int a = cha(s2);
            if (a == -1)
            {
                cout << -1;
                break;
            }
            else
                cout << a << '\n';
        }
        else
        {
            string zu, biao;
            while (zu.size() < s1.size() && s1[zu.size()] != '[')zu += s1[zu.size()];
            biao = s1.substr(zu.size() + 1, s1.size() - zu.size() - 2);
            Zu zu_now = *mo[zu];
            int a = cha(biao);
            if (a == -1 || a >= zu_now.v.size())
            {
                cout << -1;
                break;
            }
            else
            {
                int b = cha(s2);
                if (b == -1)
                {
                    cout << -1;
                    break;
                }
                (*mo[zu]).v[a] = b;
            }
        }
    }
    return 0;
}

你自己看着办吧,AC代码


|