92pts,WA on #8,求调,玄关

P2830 写程序

Eason20120229 @ 2024-10-17 19:36:03

玄关

#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)) {
        if (opt == "") {
            continue;
        }
        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 Yewenxuanccw @ 2024-10-25 21:49:20

@Eason20120229 what this?!


by 某个新手 @ 2024-10-29 20:25:26

@Eason20120229 给你一组hack数据

int a12[11]
cout a12[10]

by 某个新手 @ 2024-11-01 18:21:19

@Alex_Wei @离散小波变换° @_bzy


by Eason20120229 @ 2024-11-02 08:29:05

@某个新手 这hack有问题吧\ a是指一个数组的名称(不一定是a,也有可能是别的字母或者多个字母,总之长度不超过10)


|