iQiyi September 15th, 2018 written test programming topic AC code

These days have been concerned about the work, the brothers do the pen test, I also followed the brush, including Huawei, Baidu, Tencent, Ali, etc. The company's written test questions have been done, basically help the brothers get the qualification for the interview, but because the density is too large, and they have to sort out the study notes of leetcode, so many problems have not been settled, today just ahead of AC completion, Simply share the two topics of iQiyi.

The first topic is a string with six symbols. The symbol is any number from character 0 to character 9. After a string is given, you can change the character at any position. It is intended to make the sum of the first three characters equal to the sum of the last three characters, and to require as few characters as possible. At the beginning, only 82% can be passed, mainly considering only one side change, and both sides can be changed at the same time. The following is my AC code, written super simple, just sent a blog after the written test, a little hasty, in fact, can do a lot of optimization:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int res;
    string str = "";
    cin >> str;

    int left = str[0]+str[1]+str[2];
    int right = str[3]+str[4]+str[5];
    int dist = abs(left- right);

    if (!dist) {
        cout << dist << endl;
        return 0;
    }

    if (str[0] > str[1]) {
        swap (str[0], str[1]);
    }
    if (str[0] > str[2]) {
        swap (str[0], str[2]);
    }
    if (str[1] > str[2]) {
        swap (str[1], str[2]);
    }

    if (str[3] > str[4]) {
        swap (str[3], str[4]);
    }
    if (str[3] > str[5]) {
        swap (str[3], str[5]);
    }
    if (str[4] > str[5]) {
        swap (str[4], str[5]);
    }

    int minnums = 3;
    int nums = 0;

    int tmp = dist;
    while (tmp>0 && nums<3) {
        tmp -= ('9'-str[nums]);
        ++nums;
    }
    minnums = min(nums, minnums);

    tmp = dist;
    nums = 0;
    while (tmp>0 && nums<3) {
        tmp -= (str[2-nums]-'0');
        ++nums;
    }
    minnums = min(nums, minnums);

    tmp = dist;
    nums = 0;
    while (tmp>0 && nums<3) {
        tmp -= ('9'-str[nums+3]);
        ++nums;
    }
    minnums = min(nums, minnums);

    tmp = dist;
    nums = 0;
    while (tmp>0 && nums<3) {
        tmp -= (str[5-nums]-'0');
        ++nums;
    }
    minnums = min(nums, minnums);

    if (minnums == 3) {
        if (('9'-str[0]+str[5]-'0')>tmp || (str[3]-'0'+'9'-str[3])>tmp) {
            minnums = 2;
        }
    }

    cout << minnums << endl;
    return 0;
}

The second topic is very simple, that is, given a group of food, each The number of foods tells that one person eats one food a day, or adds one food. Finally, after M days, what is the ranking of a particular food portion?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    vector<int> A;
    int N = 0, M = 0, P = 0;
    cin >> N >> M >> P;

    for (int i=0; i<N; ++i) {
        int ai = 0;
        cin >> ai;
        A.push_back(ai);
    }

    for (int i=0; i<M; ++i) {
        char ab = 0;
        int num = 0;
        cin >> ab >> num;

        if (num <= N && num >= 0) {
            if (ab == 'A') {
                ++A[num-1];
            } else if (A[num-1] > 0) {
                --A[num-1];
            }
        }
    }

    int res = 1;
    for (int i=0; i<N; ++i) {
        if (i != (P-1) && A[i]>A[P-1]) {
            ++res;
        }
    }

    cout << res << endl;

    for (int i=0; i<A.size(); ++i) {
        cout << A[i] << " ";
    }
    cout << endl;
    return 0;
}