【笔试题】网易2018秋招内推笔试

彩色的砖块

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main() {
    string str;
    getline(cin, str);
    unordered_map<char, int> m;
    for (int i = 0; i < str.size(); i++) {
        if(m.size() > 2) {
            cout << 0;
            return 0;
        }
        m[str[i]]++;
    }
    cout << m.size();
    return 0;
}

等差数列

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int n = 0;
    //getline(cin, n);
    cin >> n;
    getchar();
    vector<int> nums(n, 0);
    for (int i = 0; i < n; i++)   // note here
        cin >> nums[i];
    sort(nums.begin(), nums.end());
    int d = nums[0] - nums[1];
    for (int i = 1; i < n - 1; i++) {
        if (nums[i] - nums[i + 1] != d) {
            cout << "Impossible";
            return 0;
        }
    }
    cout << "Possible";
    return 0;
}

交错01串

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
    string str;
    getline(cin, str);
    int n = str.size();
    int curlen = 1, maxlen = 0, i = 0;
    while (i < n - 1) {
        while (i < n - 1 && str[i] != str[1 + i]) {
            curlen++;
            i++;
        }
        i++;
        maxlen = max(maxlen, curlen);
        curlen = 1;
    }
    cout << maxlen;
    return 0;
}

操作序列

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int n = 0;
    cin >> n;
    getchar();
    vector<int> nums(n, 0);
    for (int i = 0; i < n; i++)   // note here
        cin >> nums[i];
    
    vector<int> res(n, 0);
    int l = 0, r = n - 1, i = n - 1;
    while(i >= 0) {
        res[l++] = nums[i--];
        if (i >= 0)
            res[r--] = nums[i--];
    }
    
    for (int i = 0; i < n - 1; i++) {
        cout << res[i] << ' ';
    }
    cout << res.back();
    return 0;
}

独立的小易

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int x = 0, f = 0, d = 0, p = 0;
    cin >> x >> f >> d >> p;
    if (d < f * x) cout << (int) d / x;
    else if ( d == f * x) cout << f;
    else cout << (int) (d - f * x) / (p + x) + f;
    return 0;
}

 堆棋子

疯狂队列

dfs的解法是做题的时候写的,相当蠢了,只40%ac, 看下面那个解法,我做的时候怎么没想出来,感觉不是难题啊啊啊啊啊

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void dfs(vector<vector<int> > &res, vector<int> &out, vector<bool> &visited, vector<int> heights, int level) {
    int n = heights.size();
    if ((int)out.size() == n) {
        res.push_back(out);
    }
    else {
        for (int i = 0; i < n; i++) {
            if(!visited[i]) {
                out.push_back(heights[i]);
                visited[i] = true;
                dfs(res, out, visited, heights, level + 1);
                out.pop_back();
                visited[i] = false;
            }
        }
    }
}

int main() {
    int n = 0;
    cin >> n;
    getchar();
    vector<int> heights(n, 0);
    for (int i = 0; i < n; i++)   // note here
        cin >> heights[i];
    
    vector<vector<int> > res;
    vector<int> out;
    vector<bool> visited(n, false);
    dfs(res, out, visited, heights, 0);
    
    int maxabs = 0, curabs = 0, row = res.size(), col = res[0].size();
    for (int i = 0; i < row; i++) {
        curabs = 0;
        for (int j = 1; j < col; j++) {
            curabs += abs(res[i][j-1] - res[i][j]);
        }
        maxabs = max(curabs, maxabs);
    }
    cout << maxabs;
    return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n = 0;
    cin >> n;
    getchar();
    vector<int> heights(n, 0);
    for (int i = 0; i < n; i++)
        cin >> heights[i];
    sort(heights.begin(), heights.end());
    
    vector<int> res(n, 0);
    res[n/2] = heights.back();
    int l = n/2 - 1, r = n/2 + 1, i = 0, j = n - 2;
    while (l >= 0 || r < n) {
        if (l >= 0) res[l--] = heights[i++];
        if (r < n) res[r++] = heights[i++];
        if (l >= 0) res[l--] = height[j--];
        if (r < n) res[r++] = height[j--];
    }
    
    int num = 0;
    for (int i = 0; i < n - 1; i++)
        num += abs(res[i] - res[i+1]);
    cout << num;
    return 0;
}

小易喜欢的数列

猜你喜欢

转载自blog.csdn.net/YC_cd/article/details/81566979
今日推荐