英伟达 2019校园招聘Web Services-9.04

版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82431762

1-1.png
1-2.png
1-3.png
约瑟夫环问题

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>

using namespace std;


/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
int L(int n, int m) {
    int i, res;
    int a[1001];
    int dead = 0;          //表示已经死了多少人
    int count = 0;           //num模拟没有被杀的人的喊数
    for (i = 1; i<=n; i++){//开始时每个人都可以报数,为了能得到最后一个人的编号,我们让初始值为i下标
        a[i] = i;
    }
    for (i = 1;; i++){
        if (i > n){//如果大于总人数,我们就从头开始
            i = i%n;
        }
        if (a[i] == 0)//如果当前这个人死亡,跳过
          continue;
        if (a[i] > 0)//如果当前这个人没有死,就报数
          count++;
        if (m == count && dead != n-1){ //如果当前这个人报的数等于k 并且没有已经死亡n-1个人
            count = 0;
            a[i] = 0;//死亡的人置为0
            dead++;
            //cout << i << endl;
        }else if(m == count && dead == n-1){  //如果这个人报数等于k,并且已经死了n-1个人,说明当前这个人就是最后的一个活着的了。。
            res = a[i];
            break;
        }
    }
    return res;
}
/******************************结束写代码******************************/


int main() {
    int res;

    int _n;
    cin >> _n;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');


    int _m;
    cin >> _m;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');



    res = L(_n, _m);
    cout << res << endl;

    return 0;

}

后两题还没想好,后续补
2-1.png
2-2
2-3.png

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>

using namespace std;


/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
int minSwapTime(vector < int > values) {


}
/******************************结束写代码******************************/


int main() {
    int res;

    int _values_size = 0;
    cin >> _values_size;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    vector<int> _values;
    int _values_item;
    for(int _values_i=0; _values_i<_values_size; _values_i++) {
        cin >> _values_item;
        cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
        _values.push_back(_values_item);
    }



    res = minSwapTime(_values);
    cout << res << endl;

    return 0;

}

3-1.png
3-2.png
样例输入

5 5
0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
2
0 4
2
4 4 

样例输出

8

求两点之前最短路径问题

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>

using namespace std;


/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^
******************************开始写代码******************************/
int shortestDistance(vector < vector < int > > map, vector < int > start, vector < int > dest) {


}
/******************************结束写代码******************************/


int main() {
    int res;

    int _map_rows = 0;
    int _map_cols = 0;
    cin >> _map_rows >> _map_cols;
    vector< vector < int > > _map(_map_rows);
    for(int _map_i=0; _map_i<_map_rows; _map_i++) {
        for(int _map_j=0; _map_j<_map_cols; _map_j++) {
            int _map_tmp;
            cin >> _map_tmp;
            _map[_map_i].push_back(_map_tmp);
        }
    }
    int _start_size = 0;
    cin >> _start_size;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    vector<int> _start;
    int _start_item;
    for(int _start_i=0; _start_i<_start_size; _start_i++) {
        cin >> _start_item;
        cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
        _start.push_back(_start_item);
    }


    int _dest_size = 0;
    cin >> _dest_size;
    cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    vector<int> _dest;
    int _dest_item;
    for(int _dest_i=0; _dest_i<_dest_size; _dest_i++) {
        cin >> _dest_item;
        cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
        _dest.push_back(_dest_item);
    }



    res = shortestDistance(_map, _start, _dest);
    cout << res << endl;

    return 0;

}

猜你喜欢

转载自blog.csdn.net/whl_program/article/details/82431762