牛客网——华为题库(81~90)


81.字符串字符匹配

#include <bits/stdc++.h>

using namespace std;

bool process(string s1, string s2){
    
    
    unordered_set<char> s;

    for(int i = 0; i < s1.size(); i++){
    
    
        s.insert(s1[i]);
    }
    for(int i = 0; i < s2.size(); i++){
    
    
        if(s.find(s2[i]) != s.end())
            s.erase(s2[i]);
    }
    
    if(s.empty()){
    
    
        return true;
    }
    
    return false;
}

int main(){
    
    
    string _short = "";
    string _long = "";
    cin >> _short;
    cin >> _long;
    
    bool res = process(_short, _long);
    if(res)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    
    return 0;
}

82.将真分数分解为埃及分数

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    string s = "";
    while(cin >> s){
    
    
        //法一
        string res = "";
        int idx = s.find('/');
        string z = s.substr(0, idx);
        string m = s.substr(idx + 1);
        int n = stoi(z);
        while(n--){
    
    
            res += "1/" + m + "+";
        }
        
        res = res.substr(0, res.size() - 1);
        
        cout << res << endl;
    }
    
    return 0;
}

83.二维数组操作

#include <bits/stdc++.h>

using namespace std;

bool initExcel(int m, int n){
    
    
    return m <=9 && n <= 9;
}

bool swapXY(int x1, int y1, int x2, int y2, int m, int n, vector<vector<int>>& vec){
    
    
    if(x1 < m && x2 < m && y1 < n && y2 < n){
    
    
        //int tmp = vec.at(x1).at(y1);
        //vec.at(x1).at(y1) = vec.at(x2).at(y2);
        //vec.at(x2).at(y2) = tmp;
        return true;
    }
    
    return false;
}

bool insertRows(int insertRow, int m, int n, vector<vector<int>>& vec){
    
    
    if(insertRow < 0 || insertRow >= m || m == 9){
    
     //
        return false;
    }
    else{
    
    
        //vector<int> tmp(n, 0);
        //vec.insert(vec.begin() + insertRow - 1, tmp);
        //vec.pop_back(); //恢复初始化时的大小
        return true;
    }
}

bool insertCols(int insertCol, int m, int n, vector<vector<int>>& vec){
    
    
    if(insertCol < 0 || insertCol >= n || n == 9){
    
     //
        return false;
    }
    else{
    
    
        //vector<int> tmp(m, 0);
        //vec.insert(vec.begin() + insertCol - 1, tmp);
        //vec.pop_back(); //恢复初始化时的大小
        return true;
    }
}

bool selectXY(int selectX, int selectY, int m, int n){
    
    
    return selectX >= 0 && selectX < m && selectY >= 0 && selectY < n;
}

int main(){
    
    
    int m = 0, n = 0;
    while(cin >> m >> n){
    
    
        if(initExcel(m, n)) cout << "0" << endl;
        else cout << "-1" << endl;
                    
        vector<vector<int>> vec(m, vector<int>(n, 0));
        int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
        cin >> x1 >> y1 >> x2 >> y2;
        if(swapXY(x1, y1, x2, y2, m, n, vec)) cout << "0" << endl;
        else cout << "-1" << endl;
            
        int insertRow = 0;
        cin >> insertRow;
        if(insertRows(insertRow, m, n, vec)) cout << "0" << endl;
        else cout << "-1" << endl;
        int insertCol = 0;
        cin >> insertCol;
        if(insertCols(insertCol, m, n, vec)) cout << "0" << endl;
        else cout << "-1" << endl;
        
        int selectX = 0, selectY = 0;
        cin >> selectX >> selectY;
        if(selectXY(selectX, selectY, m, n)){
    
    
            cout << "0" << endl;
        }
        else{
    
    
            cout << "-1" << endl;
        }

    }
    
    return 0;
}

84.统计大写字母个数

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    string str = "";
    while(getline(cin, str)){
    
    
        int res = 0;
        for(char ch : str){
    
    
            if(ch >= 'A' && ch <= 'Z'){
    
    
                res++;
            }
        }
        
        cout << res << endl;
    }
    
    return 0;
}

85.最长回文子串

#include <bits/stdc++.h>

using namespace std;

bool isHuiWen(string s){
    
    
    //cout << s << endl;
    /*int left = 0, right = s.size() - 1;
    while(left < right){
        if(s[left] == s[right]){
            left++;
            right--;
        }
        else{
            return false;
        }
    }
    
    return true;*/
    
    string tmp = s;
    reverse(tmp.begin(), tmp.end());
    if(tmp == s) return true;
    else return false;
}

int main(){
    
    
    string str = "";
    while(cin >> str){
    
    
        int res = INT_MIN;
        int right = 0;
        for(int i = 0; i < str.size(); i++){
    
    
            right = i;
            while(right < str.size()){
    
    
                string tmp = str.substr(i, right - i + 1);
                if(isHuiWen(tmp)){
    
    
                    res = max(res, right - i + 1);
                }

                right++;
            }         
        }
        
        cout << res << endl;
    }
    
    return 0;
}

86.求最大连续bit数

#include <bits/stdc++.h>

using namespace std;

//求1的个数
int oneNum(int num){
    
    
    int res = 0;
    while(num){
    
             
        num = num & (num - 1);
        res++;
    }
    
    return res;
}

//n进制转换
string jinZhiTrans(int m, int n){
    
    
    if(m == 0) return "0";
    bool posFlag = false; //标记是不是负数
    if(m < 0){
    
    
        posFlag = true;
        m = -m;
    }
    
    string res = "";
    string help = "0123456789ABCDEF"; //辅助的进制转换字符串
    while(m){
    
    
        res += help[m % n];
        m /= n;
    }
    
    reverse(res.begin(), res.end());
    if(posFlag) res.insert(0, "-");
    
    return res;
}

int main(){
    
    
    int num = 0;
    while(cin >> num){
    
    
        string s = jinZhiTrans(num, 2);
        //滑动窗口
        int res = INT_MIN;
        for(int i = 0; i < s.size(); i++){
    
             
            //滑动窗口
            if(s[i] == '1'){
    
    
                int right = i;
                while(s[right] == '1'){
    
    
                    right++;
                }
                res = max(res, right - i);
            }    
        }
        
        cout << res << endl;
    }
    
    return 0;
}

87.密码强度等级

#include <bits/stdc++.h>

using namespace std;

vector<pair<int, string>> m = {
    
    
    {
    
    90, "VERY_SECURE"},
    {
    
    80, "SECURE"},
    {
    
    70, "VERY_STRONG"},
    {
    
    60, "STRONG"},
    {
    
    50, "AVERAGE"},
    {
    
    25, "WEAK"},
    {
    
    0, "VERY_WEAK"},
};

int codeLength(string s){
    
    
    if(s.size() <= 4){
    
    
        return 5;
    }
    else if(s.size() >= 5 && s.size() <= 7){
    
    
        return 10;
    }
    else if(s.size() >= 8){
    
    
        return 25;
    }
    
    return 0;
}

int isChar(string s){
    
    
    bool haveChar = false;
    bool isDaXie = false;
    bool isXiaoXie = false;
    for(char ch : s){
    
    
        if(isalpha(ch)){
    
    
            haveChar = true; //有字母
            if(isupper(ch)){
    
    
                isDaXie = true;
            }
            else if(islower(ch)){
    
    
                isXiaoXie = true;
            }
        }
    }
    
    if(!haveChar) return 0;
    else if((isDaXie && !isXiaoXie) || (!isDaXie && isXiaoXie)) return 10;
    else if(isDaXie && isXiaoXie) return 20;
    
    return 0;
}

int isDigit(string s){
    
    
    int tmp = 0;
    for(char ch : s){
    
    
        if(isdigit(ch)){
    
    
            tmp++;
        }
    }
    
    if(tmp == 0) return 0;
    else if(tmp == 1) return 10;
    else if(tmp > 1) return 20;
    
    return 0;
}

//判断符号使用:
//ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E;
int symbol(string s){
    
    
    string str = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
    int tmp = 0;
    for(char ch : s){
    
    
        if(/*strstr(str.c_str(), &ch)*/
        ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E
        ){
    
     //strstr(str.c_str(), &ch); str中查找ch
            tmp++;
        }
    }
    
    if(tmp == 0) return 0;
    else if(tmp == 1) return 10;
    else if(tmp > 1) return 25;
    
    return 0;
}

int reward(string s){
    
    
    string str = "!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
    bool ziMu = false;
    bool shuZi = false;
    bool daXie = false;
    bool xiaoXie = false;
    bool fuHao = false;
    for(char ch : s){
    
    
        if(isalpha(ch)){
    
    
            ziMu = true;
            if(isupper(ch)){
    
    
                daXie = true;
            }
            else if(islower(ch)){
    
    
                xiaoXie = true;
            }
        }
        
        if(isdigit(ch)){
    
    
            shuZi = true;
        }
        
        if(/*str.find(ch)*/
        ch>=0x21&&ch<=0x2F || ch>=0x3A&&ch<=0x40 || ch>=0x5B&&ch<=0x60 || ch>=0x7B&&ch<=0x7E
        ){
    
    
            fuHao = true;
        }
    }
    
    if(ziMu && shuZi && daXie && xiaoXie && fuHao) return 5;
    else if(ziMu && shuZi && fuHao) return 3;
    else if(ziMu && shuZi) return 2;
    
    return 0;
}

int main(){
    
    
    string s = "";
    while(cin >> s){
    
    
        int res = 0;
        res += codeLength(s);
        //cout << "res1 = " << res << endl;
        res += isChar(s);
        //cout << "res2 = " << res << endl;
        res += isDigit(s);
        //cout << "res3 = " << res << endl;
        res += symbol(s);
        //cout << "res4 = " << res << endl;
        res += reward(s);
        //cout << "res = " << res << endl;
        for(auto iter = m.begin(); iter != m.end(); iter++){
    
    
            //cout << iter->first << endl;
            if(iter->first <= res){
    
    
                cout << iter->second << endl;
                break;
            }
        }
    }
    
    return 0;
}

88.扑克牌大小

#include <bits/stdc++.h>

using namespace std;

//根据输入的字符首字母输出大小等级
int getvalue(string s){
    
     
    switch(s[0]){
    
    
        case '3': return 1; //3最小
        case '4': return 2;
        case '5': return 3;
        case '6': return 4;
        case '7': return 5;
        case '8': return 6;
        case '9': return 7;
        case '1': return 8; //用1代替10
        case 'J': return 9;
        case 'Q': return 10;
        case 'K': return 11;
        case 'A': return 12;
        case '2': return 13;
    }
    return 0;
}

int main(){
    
    
    string s = "";
    while(getline(cin, s)){
    
    
        string s1 = "", s2 = "";
        int second = 0;
        for(int i = 0; i < s.size(); i++){
    
    
            if(s[i] != '-'){
    
    
                s1 += s[i];
            }
            else{
    
    
                second = i + 1;
                break;
            }
        }
        //s1 = s.substr(0, s.find('-'));
        s2 = s.substr(second);
        
        //cout << s1 << " , " << s2 << endl;
        
        int space1 = 0, space2 = 0;//统计字符串中空格的数量 确定分别有几张牌
        for(int i = 0; i < s1.length(); i++) 
            if(s1[i] == ' ')
                space1++;
        for(int i = 0; i < s2.length(); i++)
            if(s2[i] == ' ')
                space2++;
        
        //比较大小
        if(s1 == "joker JOKER" || s2 == "joker JOKER") //如果有王炸直接输出王炸
            cout << "joker JOKER";
        
        else if(space1 ==  3 && space2 == 3){
    
     //都有3个空格,说明4张牌,说明两个都是炸弹
            if(getvalue(s1) > getvalue(s2)) //比较炸弹大小
                cout << s1 << endl;
            else
                cout << s2 << endl;
        }
        
        else if(space1 == 3){
    
     //字符串其中一个空格为3,说明一个是炸弹,输出炸弹
            cout << s1 << endl;
        }
        else if(space2 == 3){
    
     //字符串其中一个空格为3,说明一个是炸弹,输出炸弹
            cout << s2 << endl;
        }
        
        else if(space1 == space2){
    
     //没有炸弹的情况下相同类型才能比较
            if(s1 == "JOKER" || s2 == "JOKER") cout << "JOKER" << endl; //
            else if(s1 == "joker" || s2 == "joker") cout << "joker" << endl; //
            
            else if(getvalue(s1) > getvalue(s2)) //个子、对子、三个、顺子都是比较第一个大小 //
                cout << s1 << endl;
            else
                cout << s2 << endl;
        }
        
        else //无法比较
            cout << "ERROR" << endl;
    }
    
    return 0;
}

89.24点运算

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

using namespace std;

//int ADD = 0, SUB = 1, MUL = 2, DIV = 3;
vector<char> Op = {
    
    '+', '-', '*', '/'};

unordered_map<string, int> _stoi = {
    
    
    {
    
    "A", 1},
    {
    
    "2", 2},
    {
    
    "3", 3},
    {
    
    "4", 4},
    {
    
    "5", 5},
    {
    
    "6", 6},
    {
    
    "7", 7},
    {
    
    "8", 8},
    {
    
    "9", 9},
    {
    
    "10", 10},
    {
    
    "J", 11},
    {
    
    "Q", 12},
    {
    
    "K", 13}
};
unordered_map<int, string> _itos = {
    
    
    {
    
    1, "A"},
    {
    
    2, "2"},
    {
    
    3, "3"},
    {
    
    4, "4"},
    {
    
    5, "5"},
    {
    
    6, "6"},
    {
    
    7, "7"},
    {
    
    8, "8"},
    {
    
    9, "9"},
    {
    
    10, "10"},
    {
    
    11, "J"},
    {
    
    12, "Q"},
    {
    
    13, "K"}
};

vector<int> nums(4);

int calculate(int a, int b, int cal){
    
    
    switch(cal){
    
    
        case 0:
            return a + b;
            break;
        case 1:
            return a - b;
            break;
        case 2:
            return a * b;
            break;
        case 3:
            return a / b;
            break;
        default:
            return 0;
            break;
    }
}

bool dfs(int start, int sum, vector<int>& ops){
    
    
    sum = calculate(sum, nums[start], *(ops.end() - 1)); //
    
    if(start == 3 && sum == 24){
    
    
        return true;
    }
    else if(start == 3){
    
    
        return false;
    }
    
    //递归
    for(int i = 0; i < 4; i++){
    
    
        ops.push_back(i);
        if(dfs(start + 1, sum, ops)){
    
    
            return true;
        }
        ops.pop_back();//回溯
    }
    
    return false;
}

int main(){
    
    
    vector<string> str(4);
    cin >> str[0] >> str[1] >> str[2] >> str[3];
    
    for(int i = 0; i < 4; i++){
    
    
        if(str[i] == "joker" || str[i] == "JOKER"){
    
    
            cout<<"ERROR"<<endl;
            return 0;
        }
        nums[i] = _stoi[str[i]];
    }
    
    sort(nums.begin(), nums.end());
    do{
    
    
        vector<int> ops; //运算符号 数组
        for(int i = 0; i < 4; i++){
    
    
            ops.push_back(i);
            
            if(dfs(1, nums[0], ops)){
    
     //start从1开始
                cout<< _itos[nums[0]]<<Op[ops[0]]
                    << _itos[nums[1]]<<Op[ops[1]]
                    << _itos[nums[2]]<<Op[ops[2]]
                    << _itos[nums[3]]<<endl;
                return 0;
            }
            
            ops.pop_back();//回溯
        }
    }while(next_permutation(nums.begin(), nums.end())); //nums的全排列
    
    cout << "NONE" << endl;
    return 0;
}

90.合法IP

#include <bits/stdc++.h>
#include <arpa/inet.h>
#include <regex>

using namespace std;

void process(string ip){
    
    
    regex pattern("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\.){4}");//匹配0.0.0.0.-255.255.255.255.的正则表达式
    ip += "."; //正则表达式匹配的四个点,ip地址后面再加一个
    if(regex_match(ip, pattern)) //匹配函数
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    
}

//判断ip
/*判断IP地址是否合法,如果满足下列条件之一即为非法地址
数字段数不为4
存在空段,即【192..1.0】这种
某个段的数字大于255
*/
//一个ip实际上是由4个[0,255] 之间的值拼接而成, 所以验证其是否都在范围内即可
bool judgeIP(string s){
    
    
    int count = 0;
    stringstream iss(s);
    string tmp = "";
    while(getline(iss, tmp, '.')){
    
    
        if(++count > 4 || tmp.empty() || stoi(tmp) > 255 
           || stoi(tmp) < 0 || (tmp.size() > 1 && (tmp[0]=='0' || !isdigit(tmp[0]) ))
          ){
    
    
            return false;
        } 
    }
    return count == 4;
}

int main(){
    
    
    string IP = "";
    while(cin >> IP){
    
    
        /*//法一
        struct sockaddr_in sa;
        //inet_pton这个函数在成功时返回1,失败时返回0
        if(inet_pton(AF_INET, IP.c_str(), &(sa.sin_addr))) cout << "YES" << endl; //
        else cout << "NO" << endl;*/
        
        //法二
        if(judgeIP(IP)) cout << "YES" << endl;
        else cout << "NO" << endl;
        
        //法三
        //process(IP);
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/XUfengge111/article/details/125129782