牛客网——华为题库(71~80)


71.字符串通配符

#include <bits/stdc++.h>

using namespace std;

//LeetCode上的类似题 : https://leetcode.cn/problems/zheng-ze-biao-da-shi-pi-pei-lcof/

//判断c是英文字母和数字0到9
bool judge(char c){
    
    
    //isalnum() 用来判断一个字符是否是字母(包括大写字母和小写字母)或者十进制数字
    return (isalpha(c) || isalnum(c)); 
}

bool isMatch(string s, string p){
    
    
    //dp[i][j]表示s的前i个字符和p的前j个字符是否匹配
    int m = s.size() + 1, n = p.size() + 1;
    vector<vector<bool>> dp(m, vector<bool>(n, false));
    dp[0][0] = true; //空字符串和空字符串匹配
    
    for(int i = 1; i < m; i++){
    
    
        dp[i][0] = dp[i - 1][0] && s[i - 1] == '*'; //
    }
    
    for(int i = 1; i < m; i++){
    
    
        for(int j = 1; j < n; j++){
    
    
            if(s[i - 1] == '*'){
    
    
                if(dp[i - 1][j]){
    
     //将字符* 看作出现0次时,能否匹配;
                    dp[i][j] = true;
                }
                if(dp[i][j - 1]){
    
     //将字符* 看作出现1次时,能否匹配;
                    dp[i][j] = true;
                }
            }
            
            else{
    
    
                if(dp[i - 1][j - 1] && tolower(s[i - 1]) == tolower(p[j - 1])){
    
    
                    dp[i][j] = true;
                }
                //问号 '?'
                if(dp[i - 1][j - 1] && (s[i - 1] == '?' && judge(p[j - 1]))){
    
    
                    dp[i][j] = true;
                }
            }
        }
    }
    
    return dp[m - 1][n - 1];
}

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

72.百钱买百鸡问题

#include<bits/stdc++.h>

using namespace std;

int main(){
    
    
    int num = 0;
    cin>>num;
    int n1 = 0; // 鸡翁个数
    int n2 = 0; // 鸡母个数
    int n3 = 3; // 鸡雏个数
    for(n1 = 0; n1 < 20; n1++){
    
    
        for(n2 = 0; n2 < 33; n2++){
    
    
            for(n3 = 0; n3 < 100; n3 = n3 + 3){
    
     //
                if(n1 + n2 + n3 == 100 && 5*n1 + 3*n2 + n3/3 == 100){
    
    
                    cout<< n1 <<" " <<n2 <<  " " << n3<<endl;
                }
            }
        }
    }         
    return 0;
}

73.计算日期到天数转换

#include <bits/stdc++.h>

using namespace std;

//平年每一个月的天数
int pingYears[]={
    
    31,28,31,30,31,30,31,31,30,31,30,31};
//闰年每一个月的天数
int runYears[]={
    
    31,29,31,30,31,30,31,31,30,31,30,31};

bool checkRunYear(int year){
    
    
    //用来判断年是不是闰年
    if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
    
    
        return true;
    }
    
    return false;
}

int main(){
    
    
    /*输入方式一
    int a, b, c;
    cin >> a >> b >> c;*/
    
    string str = "";
    while(getline(cin, str)){
    
    
        int res = 0;
        //cout << str << endl;
        /*输入方式二
        stringstream iss(str);
        string tmp = "";
        while(getline(iss, tmp, ' ')){
            //cout << tmp << endl;        
        }*/
        
        //输入方式三
        regex patten("^([^ ]*) ([^ ]*) ([^ ]*)$");
		smatch subMatch;
		if (regex_match(str, subMatch, patten)){
    
     //
            int year = stoi(subMatch[1]);
            int month = stoi(subMatch[2]);
            int day = stoi(subMatch[3]);          
            //cout << year << month << day << endl;
            
            //判断是平年还是闰年
            if(checkRunYear(year)){
    
    
                //是闰年
                for(int i = 0; i < month - 1; i++){
    
    
                    res += runYears[i]; //先计算前 month - 1 个月的天数
                }
                res += day; //加上本月已过的天数
            }
            else{
    
    
                //是平年
                for(int i = 0; i < month - 1; i++){
    
    
                    res += pingYears[i]; //先计算前 month - 1 个月的天数
                }
                res += day; //加上本月已过的天数
            }
            
        }
        
        cout << res << endl;
    }
    
    return 0;
}

74.参数解析

#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    string str = "";
    while(getline(cin, str)){
    
    
        int num = 0; //参数个数统计
        for(int i = 0; i < str.size(); i++){
    
    
            if(str[i] == '"'){
    
    
                i++; //
                
                while(str[i] != '"'){
    
    
                    i++; //
                }
            }
            
            else if(str[i] == ' '){
    
    
                num++;
            }
        }
        //输出
        cout << num + 1 << endl; // num + 1
        
        for(int i = 0; i < str.size(); i++){
    
    
            if(str[i] == '"'){
    
    
                i++;
                
                while(str[i] != '"'){
    
    
                    cout << str[i]; //
                    i++;
                }
            }
            
            else if(str[i] == ' '){
    
    
                cout << endl; //
            }
            
            else{
    
     //为字母 直接输出
                cout << str[i];
            }
        }
        
    }
    
    return 0;
}

75.公共子串计算

#include <bits/stdc++.h>

using namespace std;

void process(string s1, string s2, int& res){
    
    
    //dp[i][j]表示s1的前i个和s2的前j个字符的最大公共字串长度
    vector<vector<int>> dp(s1.size() + 1, vector<int>(s2.size() + 1, 0));
    int maxLength = INT_MIN;
    
    for(int i = 1; i <= s1.size(); i++){
    
    
        for(int j = 1; j <= s2.size(); j++){
    
    
            if(s1[i - 1] == s2[j - 1]){
    
    
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else{
    
    
                dp[i][j] = 0 ; //该位置为0
            }
            
            if(dp[i][j] > maxLength){
    
    
                maxLength = dp[i][j]; //更新最大长度
            }
        }
    }
    
    res  = maxLength;
}

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

76.尼科彻斯定理

#include <bits/stdc++.h>

using namespace std;

//验证尼科彻斯定理,即:任何一个整数m的立方都可以写成m个连续奇数之和。
void process(int num){
    
    
    int pow = num * num * num;
    for(int i = 1; i < pow; i++){
    
     //从1开始找到pow  //第一个数为i
        // 等差数列和:num * (i + (i + (num - 1) * 2)) / 2 = num * i + num * (num - 1)
        if(num * i + num * (num - 1) == pow){
    
     //比较等差数列和与三次幂是否相等
            cout << i; //相等开始输出连续num个数字
            for(int j = 1; j < num; j++){
    
     // num个连续奇数
                cout << "+" << i + 2 * j;
            }
            cout << endl;
            break;
        }
    }
    
    //第一个数等于m∗m−(m−1),那我们只要从这个数开始连续遍历输出m个奇数即可
    /*int odd = num * num - (num - 1);
    cout << odd;
    for(int i = 1; i < num; i++){
        cout << "+" << odd + 2 * i;
    }
    cout << endl;*/
}

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

77.火车进站

#include <bits/stdc++.h>
//火车站只有一个方向进出 ---- 栈
using namespace std;

vector<vector<int>> res;
vector<int> path;

void backtracking(vector<int>& nums, vector<bool> used){
    
    
    //终止条件
    if(path.size() == nums.size()){
    
    
        res.push_back(path);
    }
    
    for(int i = 0; i < nums.size(); i++){
    
    
        if(used[i]) continue;
        
        used[i] = true;
        path.push_back(nums[i]);
        backtracking(nums, used); //
        
        path.pop_back();
        used[i] = false;
    }
}

//全排列 :LeetCode 46. 全排列
vector<vector<int>> permute(vector<int>& nums) {
    
    
    if(nums.empty()) return res;
    
    vector<bool> used(nums.size(), false);
    backtracking(nums, used);
    
    return res;
}

// //根据入栈序列vec 判断 出栈序列tmp是否正确
bool check(vector<int> vec, vector<int> tmp){
    
    
    stack<int> st;
    int i = 0, j = 0;
    for(i = 0; i < vec.size(); i++){
    
    
        //依次将原始数组入栈
        st.push(vec[i]);
        //如果栈顶元素等于出栈序列元素,则栈顶元素出栈并出栈序列下标加一
        while(j < vec.size() && !st.empty() && tmp[j] == st.top()){
    
     
            //依次判断tmp中每个序列的值是否与栈顶相等
            st.pop();
            j++;
        }
    }
    
    return st.empty(); //
}

int main(){
    
    
    int n = 0;
    while(cin >> n){
    
    
        vector<int> vec(n, 0);
        for(int i = 0; i < n; i++){
    
    
            int num = 0;
            cin >> num;
            vec[i] = num;
        }
        
        //法一
        vector<int> tmp = vec; //开一个临时数组,做全排列 next_permutation
        sort(tmp.begin(), tmp.end());
        do{
    
    
//             全排列
//             for(int i = 0; i < tmp.size(); i++){
    
    
//                 cout << tmp[i] << "";
//             }
//             cout << endl;
            
            if(check(vec, tmp)){
    
    
                for(int i = 0; i < n; i++){
    
    
                    cout << tmp[i] << " "; //cout << tmp[i] << " \n"[i == tmp.size() - 1];
                }
                cout << endl;
            }
        }while(next_permutation(tmp.begin(), tmp.end())); //自动会按照字典序排序输出 全排列结果
        
        
        /*//法二
        //牛客网会超时
        vector<int> tmp = vec; //开一个临时数组,做全排列 permute(tmp)
        sort(tmp.begin(), tmp.end()); //先排序
        vector<vector<int>> quanPaiLie = permute(tmp);
        for(int j = 0; j < quanPaiLie.size(); j++){
            vector<int> tmp = quanPaiLie[j];
            if(check(vec, tmp)){
                for(int i = 0; i < n; i++){
                    cout << tmp[i] << " "; //cout << tmp[i] << " \n"[i == tmp.size() - 1];
                }
                cout << endl;
            }
        }*/
    }
    
    return 0;
}

80.整型数组合并

#include <bits/stdc++.h>

using namespace std;

void process(vector<int> nums1, vector<int> nums2, vector<int>& res){
    
    
    int m = nums1.size(), n = nums2.size();
    int i = 0, j = 0;
    while(i < m || j < n){
    
    
        if(j == n){
    
    
            res.push_back(nums1[i++]);
        }
        else if(i == m){
    
    
            res.push_back(nums2[j++]);
        }
        
        else if(nums1[i] < nums2[j]){
    
    
            res.push_back(nums1[i++]);
        }
        else if(nums1[i] > nums2[j]){
    
    
            res.push_back(nums2[j++]);
        }
        else{
    
    
            res.push_back(nums1[i]);
            i++;
            j++;
        }
    }
    
    sort(res.begin(), res.end());
    
    /*for(int i = 0; i < res.size(); i++){
        cout << res[i];
    }*/
}

int main(){
    
    
    /*法一
    int m = 0;
    cin >> m;
    vector<int> nums1;
    for(int i = 0; i < m; i++){
        int tmp = 0;
        cin >> tmp;
        nums1.push_back(tmp);
    }
    
    int n = 0;
    cin >> n;
    vector<int> nums2;
    for(int i = 0; i < n; i++){
        int tmp = 0;
        cin >> tmp;
        nums2.push_back(tmp);
    }
    
    vector<int> res;
    process(nums1, nums2, res);
    
    set<int> st; //使用set存,set有自动排序的特性,且不允许有重复元素
    for(int i = 0; i < res.size(); i++){
        st.insert(res[i]);
    }
    for(auto iter = st.begin(); iter != st.end(); iter++){
        cout << *iter;
    }
    */
    
    //法二
    int m,n;
    set<int> gather; //使用set存,set有自动排序的特性,且不允许有重复元素
    while(cin>>m){
    
    
        int num1,num2;
        for(int i=0;i<m;i++){
    
    
            cin>>num1;
            gather.insert(num1);
        }
        cin>>n;
        for(int i=0;i<n;i++){
    
    
            cin>>num2;
            gather.insert(num2);
        }
        for(auto it=gather.begin();it!=gather.end();it++){
    
    
            cout<<*it;
        }
    }
    
    return 0;
}

猜你喜欢

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