其他分享
首页 > 其他分享> > 2021-09-24meituan-012小美的书架

2021-09-24meituan-012小美的书架

作者:互联网

在这里插入图片描述
输入:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/FvoBGh
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

输入:
5 5 10
1 1 4
1 2 3
1 3 1
2 1
4 1
5 2
4 3
4 5
3 1
4 2
输出:
4
-1
-1
3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/FvoBGh
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
在这里插入图片描述

#include<cstdio>
#include<cstring>

using namespace std;

int main() {
    int m, n, q;
    scanf("%d %d %d", &m, &n, &q);
    int book[m + 1]; // 1~m号书在哪行书架上,0表示未上架,-1表示借出
    memset(book, 0, sizeof(book));
    int locked[n + 1]; // 1~n行书架是否加锁
    memset(locked, 0, sizeof(locked));
    for (int i = 0; i < q; ++i) {
        int c;
        scanf("%d", &c);
        switch(c) {
            int x, y;
            case 1:
                scanf("%d %d", &x, &y);
                if (locked[y] == 0 && (book[x] == 0 || book[x] > 0 && locked[book[x]] == 0))
                    book[x] = y;
                break;
            case 2:
                scanf("%d", &y);
                locked[y] = 1;
                //1表示对这一行的书本进行加锁
                break;
            case 3:
                scanf("%d", &y);
                locked[y] = 0;
                break;
            case 4:
                scanf("%d", &x);
                if (book[x] > 0 && locked[book[x]] == 0) { // 上架且未上锁的书才可以借出
                    printf("%d\n", book[x]);
                    book[x] = -1; // 借出
                }
                else
                    printf("-1\n");
                break;
            case 5:
                scanf("%d", &x);
                if (book[x] == -1)
                    book[x] = 0; // 还书,变为未上架状态
                break;
        }
    }
    return 0;
}

/*作者:xgn911
链接:https://leetcode-cn.com/problems/FvoBGh/solution/cyong-shu-zu-shi-xian-ha-xi-by-xgn911-bh8i/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/

标签:24meituan,locked,书架,int,scanf,09,美的,book,编号
来源: https://blog.csdn.net/weixin_51187533/article/details/120452046