【华为2019年校园招聘】2019-4-10 软件题

题目

我的代码

#include <iostream>
#include <utility>
#include <queue>
#include <math.h>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    int * matrix = new int[N*M];
    for(int i = 0; i < N; i++){
        for(int j = 0; j < M; j++){
            int x; scanf("%d",&x);
            matrix[i*M+j] = x;
        }
    }
    int X, Y, Z, W;
    cin >> X >> Y >> Z >> W ;
    queue<pair<int, int>> q;
    pair<int, int> p(X,Y);
    q.push(p);
    int result = 0;
    while(!q.empty()) {
        pair<int, int> temp(q.front());
        int row = temp.first, col = temp.second;
        q.pop();
        if(row == Z && col == W) {
            result++;
        } else {
            if(row+1 < N && matrix[(row+1)*M + col] > matrix[row*M + col])
                q.push(make_pair(row+1, col));
            if(row-1 >= 0 && matrix[(row-1)*M + col] > matrix[row*M + col])
                q.push(make_pair(row-1, col));
            if(col+1 < M && matrix[row*M + col + 1] > matrix[row*M + col])
                q.push(make_pair(row, col+1));
            if(col-1 >= 0 && matrix[row*M + col - 1] > matrix[row*M + col])
                q.push(make_pair(row, col-1));
        }   
    }
    result %= int(pow(10, 9));
    cout << result<< endl;
    return 0; 
}

结果

到现在不太清楚为啥没过。

第1题

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int N;
    cin >> N;
    string s;
    int length;
    int a, b;
    vector<string> vec;
    for(int i=0; i<N; i++) {
        cin >> s;
        length = s.size();
        a = length%8;
        b = length / 8 ;
        int j=0;
        for(; j<b; j++) {
            vec.push_back(s.substr(j*8, 8));
        }
        if(a>0) {
            int c = length-j*8;
            string str = s.substr(j*8, c);
            for(int k=0; k<8-c; k++) str+="0";
            vec.push_back(str);
        } 
    }
    sort(vec.begin(), vec.end());
    vector<string>::iterator st;
    for(st=vec.begin(); st!=vec.end(); st++){
        cout<< *st << " ";
    }
    return 0;
}

代码写得不太好,但是100%全过了

第二题

没做

猜你喜欢

转载自blog.csdn.net/L_bic/article/details/89203006