2020 Spring recruit Huawei written February 26

Time is two hours, a total of three programming problems.

Title first pass effect:

  Enter the number of a type of int, it is determined how many bit stream "010," a second "101" index (the subscripts from the low-order number).

  Such as: Input: 21   

    Output 20

       The reason: 21 in binary is 0,000,000,000,000,000 0,000,000,000,010,101        

       A total of two "101" (two "101" may overlap), and the first subscript 0, and the second subscript is 2, returns 20

Submit code as follows

 1 int main(){
 2     int num;
 3     while(cin >> num){
 4         int tag = 5, tag1 = 2;// 分别是101  和  010
 5         int times = 0, numcnt = 30, firstindex = -1;
 6         while(numcnt --){
 7             if((num & tag) == tag && (num & tag1) == 0){
 8                 times ++;
 9                 if(firstindex == -1)
10                     firstindex = 29 - numcnt;
11             }
12             tag <<= 1;
13             tag1 <<= 1;
14         }
15         cout << times << " " << firstindex << endl;
16     }
17     return 0;
18 }
View Code

 

The second effect track Title:

BACKGROUND: a database record (comprising a plurality of fields: numeric, string) into a string as input, the classification task now is open to different fields, each field contents and the number of the output field.

String input line, which is divided into a plurality of segments, the conditions to be satisfied by the input string:

  1. The input string with no spaces
  2. Between different fields separated by commas
  3. If a field within a comma ( ",") or a quotation mark ( "" "), the field will be end-quotation marks (" ""), and writing marks "in the field" (two quotation marks)

If the input string has a problem, the output ERROR;

Otherwise, the output number of fields, and then outputs each field (each line)

My thinking is: because each field quotes are paired, so meet comma when the judge at this time whether the quotes in pairs. If pairs, indicating that the internal field is a comma comma; on the contrary, the number of comma-separated two fields.

Submit code as follows:

 1 int main(){
 2     int num;
 3     string inStr;
 4     while(getline(cin,inStr)){
 5         if(inStr.size() == 0){
 6             cout << 0 << endl;
 7             continue;
 8         }
 9         int lastIndex = -1;
10         stack<char> charStack;
11         vector<string> strVec;
12         for(int i = 0; i < inStr.size(); ++ i){
13             if(inStr[i] == '"'){
14                 charStack.empty() ? charStack.push('"') : charStack.pop();
15             }
16             if(inStr[i] == ',' && charStack.empty()){
17                 strVec.push_back(inStr.substr(lastIndex+1, i-lastIndex-1));
18                 lastIndex = i;
19             }
20         }
21         if(!charStack.empty()){
22             cout << "ERROR" <<endl;
23             continue;
24         }
25         strVec.push_back(inStr.substr(lastIndex+1, inStr.size()-lastIndex-1));
26         cout << strVec.size() << endl;
27         for(int i = 0; i < strVec.size(); ++ i){
28             if(strVec[i].size() == 0 || (strVec[i].size() == 2 && strVec[i][0] == '"' && strVec[i][1] == '"'))
29                 cout << "--" <<endl;
30             else if(strVec[i][0] == '"'){
31                 bool flag = false;
32                 for(auto it = strVec[i].begin()+1; it != strVec[i].end(); ++ it){
33                     if(*it == '"'if34) {
                         (flag){
35                             printf("\"");
36                             flag = false;
37                         }
38                         else
39                             flag = true;
40                     }
41                     else
42                         printf("%c", *it);
43                 }
44                 printf("\n");
45             }
46             else
47                 cout << strVec[i] << endl;
48         }
49     }
50     return 0;
51 }
View Code

 

 

The third topic to the effect:

BACKGROUND: Friends recommendation function, A and B are friends, B and C are friends, A and C are not friends, then C is 2 degrees friend A; and familiarity of A and B is m, familiarity B and C is n , the degree of recommendation for the a and C m + n;

Input: the number of test cases T;

   Then enter each test: user number m, a specific user id, the required degree of friends t, the number n of known friends

              The next n input lines, the contents of each line is: familiarity two user id User id 1 2

Output: output t of the first number of a particular user's friends, not the output 1; if so, outputting a next user id, as recommended by descending (if the same degree of recommendation, according to id from small to large)

At that time the idea is: dijkstra algorithm to find a specific user t of friends, then sort the output. (However, the impression that topic when the two distances have the same path, with the highest degree of recommendation that is the first one did not make it clear that it may be I did not understand the meaning of the questions clearly. I was part of the code is commented out and commit it. )

Was submitted by 40 per cent, we submit the following code:

int T, userCnt, userId, friendVal, pairCnt;

int dist[50];
int friendValSum[50];
int routeMatrix[50][50];
int valMatrix[50][50];
void dijkstra(int root){
    memset(dist, 0x7f, sizeof(dist));
    memset(friendValSum, 0, sizeof(friendValSum));
    for(int i = 0; i < userCnt; ++ i)
        dist[i] = routeMatrix[root][i];
    for(int i = 0; i < userCnt; ++ i)
        friendValSum[i] = valMatrix[root][i];
    dist[root] = 0;
    vector<bool> flagVec(50,false);
    flagVec[root] = true;
    for(int j = 1; j < userCnt; ++ j){
        int minDis = INF, v = -1;
        for(int i = 0; i < userCnt; ++i){
            if(!flagVec[i] && dist[i] < minDis){
                minDis = dist[i];
                v = i;
            }
        }
        if(v == -1 || minDis > friendVal)
            return;
        flagVec[v] = true;
        for(int i = 0; i < userCnt; ++ i){
            if(!flagVec[i] && routeMatrix[v][i] + dist[v] < dist[i]){
                dist[i] = routeMatrix[v][i] + dist[v];
                friendValSum [i] = valMatrix [v] [i] + friendValSum [v];
            }
            /*else if(!flagVec[i] && routeMatrix[v][i] + dist[v] == dist[i] && valMatrix[v][i] + friendValSum[v] > friendValSum[i]){
                friendValSum [i] = valMatrix [v] [i] + friendValSum [v];
            }*/
        }
    }
}

typedef struct NODE{
    int id, val;
    NODE(int d, int v):id(d),val(v){}
}node;
bool cmp(node a, node b){
    if(a.val != b.val)
        return a.val > b.val;
    else
        return a.id < b.id;
}
int main(){
    int tmpSt, tmpEnd, tmpVal;
    cin >> T;
    while(T--){
        cin >> userCnt >> userId >> friendVal;
        cin >> pairCnt;
        memset(routeMatrix, 0x7f, sizeof(routeMatrix));
        memset(valMatrix, 0x7f, sizeof(valMatrix));
        for(int i = 0; i < pairCnt; ++ i){
            scanf("%d %d %d", &tmpSt, &tmpEnd, &tmpVal);
            routeMatrix[tmpSt][tmpEnd] = 1;
            routeMatrix[tmpEnd][tmpSt] = 1;
            valMatrix [tmpSt] [tmpEnd] = tmpVal;
            valMatrix [tmpEnd] [tmpSt] = tmpVal;
        }
        dijkstra(userId);
        //if(friendVal == 0){
        //    cout << "-1" << endl;
        //    continue;
        //}
        vector<node> nodeVec;
        for(int i = 0; i < userCnt; ++ i){
            if(dist[i] == friendVal){
                nodeVec.push_back(NODE(i, friendValSum[i]));
            }
        }
        if(nodeVec.size() == 0)
            cout << "-1" << endl;
        else{
            sort(nodeVec.begin(), nodeVec.end(), cmp);
            bool flag = false;
            for(int i = 0; i < nodeVec.size(); ++i){
                flag ? printf(" ") :flag = true;
                printf("%d", nodeVec[i].id);
            }
        }
        printf("\n");
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/codewars/p/12375308.html