当前位置:网站首页>Niuke.com Huawei question bank (31~40)

Niuke.com Huawei question bank (31~40)

2022-06-22 10:09:00 wrdoct


31. Word inversion

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

using namespace std;

int main(){
    
    string str = "";
    getline(cin, str);
    str += " ";
    vector<string> vec;
    string tmp = "";
    for(char c : str){
    
        if(!isalpha(c)){
    
            if(!tmp.empty()){
    
                vec.push_back(tmp);
                tmp.clear();
            }
        }
        else{
    
            tmp += c;
        }
    }
    
    reverse(vec.begin(), vec.end());
    string res = "";
    for(int i = 0; i < vec.size(); i++){
    
        //cout << vec[i]<<endl;
        res += vec[i];
        res += " ";
    }
    res.pop_back();
    
    cout << res << endl;
    
    return 0;
}

32. Password interception

#include <bits/stdc++.h>

using namespace std;

int getLength(string str, int l, int r){
    
    while(l >= 0 && r < str.size() && str[l] == str[r]){
    
        l--;
        r++;
    }
    
    return r - l - 1;
}

// Maximum palindrome substring 
void process(string str, int& res){
    
    //dp[i][j]: character string s stay [i, j] The length of the longest palindrome substring in the range is dp[i][j].
    int n = str.size();
    vector<vector<bool>> dp(n, vector<bool>(n, false));
    for(int i = 0; i < n; i++){
    
        dp[i][i] = true;// The length of the palindrome subsequence of a character is 1.
    }
    
    for(int i = n - 1; i >= 0; i--){
    
        for(int j = i; j < n; j++){
     //
            if(str[i] == str[j]){
    
                if(j - i <= 2){
    
                    dp[i][j] = true; //
                }
                else{
    
                    dp[i][j] = dp[i + 1][j - 1];
                }
            }
            
            // Take the longest palindrome substring 
            if(dp[i][j] && j - i + 1 > res){
    
                //s = str.substr(i, j - i + 1);
                res = j - i + 1;
            }
        }
    }
    
    /*// The central expansion method  int n = str.size(); for(int i = 0; i < n; i++){ //ABA int l1 = getLength(str, i, i); //ABBA int l2 = getLength(str, i, i + 1); res = max(res, l1 > l2 ? l1 : l2); }*/
}

int main(){
    
    string str = "";
    cin >> str;
    int res = 0;
    process(str, res);
    
    cout << res << endl;
    
    return 0;
}

33. Integers and IP Address to address translation

#include <bits/stdc++.h>

using namespace std;

void process(string strIP, long decIP, long& _decIP, string& _strIP){
    
    stringstream iss(strIP);
    string sTmp = "";
    vector<long> nums; //long
    while(getline(iss, sTmp, '.')){
    
        nums.push_back(atol(sTmp.c_str()));
    }
    _decIP = nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3]; // Bit operation assembly 
    
    _strIP += to_string((decIP >> 24) & 0xff); // Take the first eight bit binary and convert it into characters  & 0xff
    _strIP += ".";
    _strIP += to_string((decIP >> 16) & 0xff);
    _strIP += ".";
    _strIP += to_string((decIP >> 8) & 0xff);
    _strIP += ".";
    _strIP += to_string(decIP & 0xff);
}

int main(){
    
    string strIP = "";
    long decIP = 0;
    cin >> strIP;
    cin >> decIP;
    
    long _decIP = 0;
    string _strIP = "";
    process(strIP, decIP, _decIP, _strIP);
    
    cout << _decIP << endl;
    cout << _strIP <<endl;
    
    return 0;
}

34. Picture arrangement

#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;

struct cmp{
    
    bool operator()(char& a, char& b){
    
        return a < b; // Small in the front, big in the back   Ascending 
    }  
};

int main(){
    
    string str = "";
    getline(cin, str);

    sort(str.begin(), str.end(), cmp());
    
    cout << str << endl;
    
    return 0;
}

35. Snake matrix

#include <bits/stdc++.h>

using namespace std;

void process(int N){
    
    /*if(N == 0){ cout << "" << endl; }*/
    // The last element in the first line  = (N)*(N + 1) / 2;
    // The first line is j Column elements  = (j) * (j + 1) / 2;
    // The first i Xing di j Column elements  =  The first i - 1 That's ok j + 1 Column  - 1;
    // The last line has only one element  =  The last element in the first line  - N - 1;
    int num = 1;
    vector<vector<int>> matrix(N, vector<int>(N, 0));
    for(int i = 0; i < N; i++){
    
        for(int j = 0; j <= i; j++){
       
            matrix[i - j][j] = num++; //
        }
    }
    
    for(int i = 0; i < N; i++){
    
        for(int j = 0; j < N - i; j++){
     //
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

int main(){
    
    int N = 0;
    cin >> N;
    
    process(N);
    
    return 0;
}

36. String encryption

#include <bits/stdc++.h>

using namespace std;

//encoder
void encoder(string key, string str, string& res){
    
    // If the word contains repeated letters , Keep only the first one 1 individual , Start the result with the new alphabet 
    vector<char> s; //s[i] The corresponding is 26 The... Of the first letters i The result of encryption of letters 
    for(int i = 0; i < key.size(); i++){
    
        key[i] = tolower(key[i]); // All lowercase 
        if(find(s.begin(), s.end(), key[i]) == s.end()) // If it's the first time to join 
            s.push_back(key[i]);
    }
    for(char c = 'a'; c <= 'z'; c++){
    
        if(find(s.begin(), s.end(), c) == s.end()) // If it's the first time to join 
            s.push_back(c);
    }
    /*for(char ch : s){ cout << ch; }*/
    
    for(int i = 0; i < str.size(); i++){
    
        if(isupper(str[i])){
    
            res += s[str[i] - 'A'] - 32; // You need to subtract... From the converted lower case letters 32
        }
        else{
    
            res += s[str[i] - 'a']; //
        }
    }
}

int main(){
    
    string key = "";
    cin >> key;
    string str = "";
    cin >> str;
    
    string res = "";
    encoder(key, str, res);
    
    cout << res << endl;
    
    return 0;
}


37. Count the total number of rabbits every month

#include <bits/stdc++.h>

using namespace std;

int process(int n){
    
    return n < 3 ? 1 : process(n - 1) + process(n - 2);
}

int main(){
    
    int n = 0;
    while(cin >> n){
    
        int res = process(n);
        
        cout << res << endl;
    }
  
    return 0;
}

38. Ask the ball to land 5 The journey after the first time and the second 5 The height of the second rebound

#include <bits/stdc++.h>

using namespace std;

void process(int num, double& distance, double& high){
    
    double tmp = (double)num;
    for(int i = 0; i < 5; i++){
               
        distance += tmp * 2;
        tmp = tmp / 2;     
    }
    distance = distance - (double)num;
    high = tmp;
}

int main(){
    
    int num = 0;
    cin >> num;
    double distance = 0.0;
    double high = 0.0;
    
    process(num, distance, high);
    
    printf("%lf\n", distance);
    printf("%lf\n", high);
    
    return 0;
}

39. Whether two IP Whether it belongs to the same subnet

#include <bits/stdc++.h>

using namespace std;

string str2bin(int num){
    
    if(num == 0) return "00000000";
    
    //cout << "num = " << num << endl;
    string binRes = "";
    string help = "0123456789ABCDEF";
    while(num){
    
        binRes += help[num % 2]; // Binary system 
        num /= 2;
    }
    reverse(binRes.begin(), binRes.end());
    while(binRes.size() < 8){
    
        binRes.insert(binRes.begin(), '0');
    } 
    //cout << "binRes = " << binRes << endl;
    return binRes;
}

bool noLegalYanMa(string zwYanMa){
    
    string res = "";
    stringstream iss(zwYanMa);
    string tmp = "";
    bool isNeg = false;
    while(getline(iss, tmp, '.')){
           
        if(tmp[0] == '-'){
    
            isNeg = true;
            tmp = tmp.substr(1, tmp.size() - 1);
        } 
        //cout << "tmp = " << tmp << endl;
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
    
            num = num * 10 + (tmp[i] - '0');
        }
        if(isNeg) num = -num;
        
        //cout << "num==" << num << endl;
        if(num < 0 || num > 255) return true;
        
        res += str2bin(num);
    }
    //cout << "res = " << res << endl;
    bool notZero = true;
    for(int j = 0; j < res.size(); j++){
    
        if(res[j] == '0') {
    
            notZero = false;
        }
        else{
    
            if(!notZero){
    
                return true;
            }
            continue;
        }   
    }
    
    return false;
}

string str2binStr(string s){
    
    string res = "";
    stringstream iss(s);
    string tmp = "";
    while(getline(iss, tmp, '.')){
    
        //cout << "tmp = " << tmp << endl; 
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
    
            num = num * 10 + (tmp[i] - '0');
        }
        //cout << "num==" << num << endl; 
        res += str2bin(num);
    }
    
    return res;
}

bool noLegalIP(string IP){
    
    string res = "";
    stringstream iss(IP);
    string tmp = "";
    bool isNeg = false;
    while(getline(iss, tmp, '.')){
    
        if(tmp[0] == '-'){
    
            isNeg = true;
            tmp = tmp.substr(1, tmp.size() - 1);
        } 
        //cout << "tmp = " << tmp << endl;
        int size = tmp.size();
        int num = 0;
        for(int i = 0; i < size; i++){
    
            num = num * 10 + (tmp[i] - '0');
        }
        if(isNeg) num = -num;
        //cout << "num==" << num << endl; 
        if(num < 0 || num > 255) return true;
    }
    
    return false;
}

string anWeiSum(string zwYanMa, string IP){
    
    string s1 = str2binStr(zwYanMa);
    string s2 = str2binStr(IP);
    cout << s1 << endl;
    cout << s2 << endl;
    
    string res = "";
    int i = s1.size() - 1;
    int j = s2.size() - 1;
    int carry = 0; // carry 
    while(i >= 0 || j >= 0){
    
        int digitA = i >= 0 ? (s1[i--] - '0') : 0; //i--
        int digitB = j >= 0 ? (s2[j--] - '0') : 0; //j--
        int sum = digitA + digitB + carry;
        carry = sum >= 2 ? 1 : 0;
        sum = sum >= 2 ? (sum - 2) : sum;
        
        res += to_string(sum);
    }
    if(carry == 1) res += "1";
    reverse(res.begin(), res.end());
    cout << res << endl;
    return res;
}

string anWeiYu(string zwYanMa, string IP){
    
    string s1 = str2binStr(zwYanMa);
    string s2 = str2binStr(IP);
    //cout << s1 << endl;
    //cout << s2 << endl;
    
    string res;
    for(int i = 0; i < 32; i++){
    
        if(s1[i] != s2[i]){
    
            res += "0";
        }
        else if(s1[i] == '0' && s2[i] == '0'){
    
            res += "0";
        }
        else{
     //s1[i] == '1' && s2[i] == '1'
            res += "1";
        }
    }
    //cout << res << endl;
    return res;
}

bool isSameZiWang(string zwYanMa, string firstIP, string secondIP){
    
    if(anWeiYu(zwYanMa, firstIP) == anWeiYu(zwYanMa, secondIP)){
    
        return true;
    }
    return false;
}

void process(string zwYanMa, string firstIP, string secondIP, int& res){
    
    if(noLegalYanMa(zwYanMa) || noLegalIP(firstIP) || noLegalIP(secondIP)){
    
        res = 1;
        return;
    }

    if(isSameZiWang(zwYanMa, firstIP, secondIP)){
    
        res = 0;
    }
    else{
    
        res = 2;
    }
    
    return;
}

int main(){
    
    string zwYanMa = "";
    string firstIP = "";
    string secondIP = "";
    getline(cin, zwYanMa);
    getline(cin, firstIP);
    getline(cin, secondIP);
    int res = 0;

    process(zwYanMa, firstIP, secondIP, res);
    //anWeiYu(zwYanMa, firstIP);
    cout << res << endl;
    return 0;
}

40. Statistical characters

#include<iostream>
#include<string>

using namespace std;

int main()
{
    
    string str = "";
    while (getline(cin, str))
    {
    
        int alphaNum = 0;
        int spaceNum = 0;
        int digitNum = 0;
        int punctNum = 0;
        
        for(char c : str){
    
            if(isalpha(c)) alphaNum++;
            else if(isspace(c)) spaceNum++;
            else if(isdigit(c)) digitNum++;
            else if(ispunct(c)) punctNum++;
        }
        
        cout << alphaNum << endl << spaceNum << endl << digitNum << endl << punctNum << endl;
    }
    
    return 0;    
}

原网站

版权声明
本文为[wrdoct]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221003201190.html