米哈游笔试(9/12)

1.括号匹配(100%)

//
// Created by Administrator on 2021/9/12 0012.
//
#include<iostream>
#include<stack>

using namespace std;

int func(string& s){
    if(s.size() == 0) return 0;
    int result = 0;
    stack<char> stack_s;
    stack_s.push(s[0]);
    for(int i=1;i<s.size();++i){
        if(s[i] == '{' || s[i] == '['){
            stack_s.push(s[i]);
        }else{

            char c = stack_s.top();
            stack_s.pop();
            if(c == '{' && s[i] == '}'){
                continue;
            }
            if(c == '[' && s[i] == ']'){
                continue;
            }
            result++;
        }
    }
    return result;
}

int main(){
    int n;
    cin >> n;
    string s;
    while(n--){
        cin >> s;
        cout << func(s) << endl;
    }
}

2.给一个3的倍数,3个3的倍数之和等于该数(100%)

//
// Created by Administrator on 2021/9/12 0012.
//
#include<iostream>

using namespace std;



int main(){
    int n;
    cin >> n;
    int m;
    while(n--){
        cin >> m;
        if(m == 3 || m == 6){
            cout << 0 << endl;
        }else{
            int d = (m/3)-2;
            int result = ((1+d)*d)/2;
            cout << result << endl;
        }
    }
}

3.象吃将(90%)

//
// Created by Administrator on 2021/9/12 0012.
//

#include<iostream>
#include<vector>
#include<queue>


using namespace std;

int func(vector<pair<int,int>>& dir,int r1,int c1,int r2,int c2){
    if(r1 <0 || r1 > 9 || c1 <0 || c1 > 8) return -1;
    if(r2 <0 || r1 > 9 || c2 <0 || c2 > 8) return -1;
    queue<pair<int,int>> queue_d;
    queue_d.push({r1,c1});
    int result = -1;
    while(!queue_d.empty()){
        result++;
        int count =queue_d.size();
        while(count--){
            pair<int,int> temp = queue_d.front();
            queue_d.pop();
            int l = temp.first;
            int r = temp.second;
            if(l == r2 && r == c2){
                return result;
            }
            for(auto d:dir){
                int i = d.first;
                int j = d.second;
                if(i+l < 0 || i+l >9 || j+r < 0 || j+r >8){
                    continue;
                }else{
                    queue_d.push({i+l,j+r});
                }
            }
        }
    }
    return -1;
}

int main(){
    int r1,c1;
    int r2,c2;
    cin >> r1;
    cin.get();
    cin >> c1;
    cin >> r2;
    cin.get();
    cin >> c2;
    vector<pair<int,int>> dir = {{-3,-2},{-3,2},{2,-3},{-2,-3},{3,-2},{3,2},{-2,3},{2,3}};
    cout << func(dir,r1,c1,r2,c2) << endl;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值