绿色模拟41ptsWA(悬赏1个关注)

P2830 写程序

ChampionCyan @ 2024-04-05 15:00:22

题目

#include <unordered_map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef vector < int > vi;
unordered_map < string, vi > mem;

void end();

int digit(string);

string inside(string);

string name(string);

int value(string);

int main() {
    string a, b;
    while (cin >> a >> b) {
        if (a == "int") {
            vi m;
            int len = value(inside(b));
            for (int i = 1; i <= len; i++)
                m.push_back(0);
            mem.insert(make_pair(name(b), m));
        } else if (a == "cout")
            printf("%d\n", value(b));
        else {
            if (value(inside(a)) >= mem[name(a)].size())
                end();
            vi v = mem[name(a)];
            int vb = value(b);
            string va = name(a);
            mem.erase(va);
            v[value(inside(a))] = vb;
            mem.insert(make_pair(va, v));
        }
    }
    exit(0);
}

void end() {
    puts("-1");
    exit(0);
}

int digit(string s) {
    int a = (int)s.length(), ret = 0;
    for (int i = 0; i < a; i++)
        if (s[i] < '0' || s[i] > '9')
            return -1;
        else
            ret = ret * 10 + s[i] - '0';
    return ret;
}

string inside(string s) {
    int l = 0, r = int(s.length()) - 2;
    string ss = "";
    while (s[l] != '[')
        l++;
    while (l < r)
        ss += s[++l];
    return ss;
}

string name(string s) {
    int i = 0;
    string ss = "";
    while (s[i] != '[')
        ss += s[i++];
    return ss;
}

int value(string s) {
    if (digit(s) >= 0)
        return digit(s);
    int v = value(inside(s));
    if (mem[name(s)].size() <= v)
        end();
    return v;
}

by 001_zip @ 2024-04-05 15:09:55

呃呃


by ChampionCyan @ 2024-04-05 15:14:20

@001_zip

我理解你的意思(大模拟太难调了,所以我才发这个帖)


by ChampionCyan @ 2024-04-05 15:16:38

记录


by MOSS_550W @ 2024-04-05 15:43:20

稍等


by MOSS_550W @ 2024-04-05 15:44:40

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

const int maxn = 100;
unordered_map<string, int> arr;

bool isOutOfBounds(int index) {
    return (index < 0 || index >= maxn);
}

int main() {
    string line;
    while (getline(cin, line)) {
        if (line.substr(0, 3) == "int") {
            // 解析数组声明语句
            int pos1 = line.find(' ');
            int pos2 = line.find('[');
            int pos3 = line.find(']');
            string arrName = line.substr(pos1 + 1, pos2 - pos1 - 1);
            int arrSize;
            if (isdigit(line[pos3 - 1])) {
                arrSize = stoi(line.substr(pos2 + 1, pos3 - pos2 - 1));
            } else {
                arrSize = arr[line.substr(pos2 + 1, pos3 - pos2 - 1)];
            }
            arr[arrName] = arrSize;
        } else if (line.substr(0, 5) == "cout ") {
            // 解析输出语句
            string varName = line.substr(5);
            if (arr.find(varName) != arr.end()) {
                cout << arr[varName] << endl;
            } else {
                cout << -1 << endl;
                break;
            }
        } else {
            // 解析赋值语句
            int pos = line.find(' ');
            string arrName = line.substr(0, pos);
            int pos2 = line.find(' ', pos + 1);
            int pos3 = line.find(' ', pos2 + 1);
            string index = line.substr(pos + 1, pos2 - pos - 1);
            string value = line.substr(pos3 + 1);
            int arrIndex;
            if (isdigit(index[0])) {
                arrIndex = stoi(index);
            } else {
                arrIndex = arr[index];
            }
            if (isOutOfBounds(arrIndex)) {
                cout << -1 << endl;
                break;
            }
            arr[arrName] = stoi(value);
        }
    }
    return 0;
}

by MOSS_550W @ 2024-04-05 15:45:20

试试(有可能会逝世)


by MOSS_550W @ 2024-04-05 15:46:44

emmm............


by ChampionCyan @ 2024-04-05 15:48:07

@MOSS_550W

我逝世


by ChampionCyan @ 2024-04-05 15:49:01

@MOSS_550W

6,你这才8分,我的分都比你高


by ChampionCyan @ 2024-04-05 15:49:39

你的记录


| 下一页