牛客网——华为题库(11~20)


11.数字颠倒

#include <iostream>
#include <string>
#include <algorithm>
//#include <math.h>

using namespace std;

int main(){
    int num;
    cin>>num;
    string res = to_string(num);
    reverse(res.begin(), res.end());
    cout<<res<<endl;
    
    return 0;
}

12.字符串反转

#include <iostream>
#include <string>
#include <algorithm>
//#include <math.h>

using namespace std;

int main(){
    string res;
    cin>>res;
    reverse(res.begin(), res.end());
    cout<<res<<endl;
    
    return 0;
}

13.句子逆序

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

using namespace std;

void process(string str, string& res){
    string tmp = "";
    vector<string> vec;
    
    int idx = 0;
    while(str[idx] == ' '){
        idx++;
    }
    str = str.substr(idx);
    str += ' '; //补一个空格,避免最后一个单词无法取出
    
    for(char c : str){
        if(c == ' '){
            if(!tmp.empty()){            
                vec.push_back(tmp);
                tmp.clear();
            }
        }
        else{
            tmp += c;//cout<<tmp<<endl;
        }
    }
    
    reverse(vec.begin(), vec.end());

    for(string s : vec){
        res += s;
        res += " ";
    }
    
    res.pop_back(); //把最后一个空格推出去
}

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

14.字符串排序

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

using namespace std;

int main(){
    int n = 0;
    cin>>n;
    vector<string> vec;
    while(n--){
        string str;
        cin>>str;
        vec.push_back(str);
    }
   
    sort(vec.begin(), vec.end());
    for(int i = 0; i < vec.size(); i++){
        cout<<vec[i]<<endl;
    }
    
    return 0;
}

15.求int型正整数在内存中存储时1的个数

#include <iostream>
#include <string>
#include <algorithm>
//#include <math.h>

using namespace std;

void process(int num, int& res){
    while(num != 0){
        res++;
        num &= (num - 1);
    }
}

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

16.购物单

//背包问题
#include <iostream>
#include <vector>

#define max(x, y) (x) > (y) ? (x) : (y)

using namespace std;

int getMax(int x, int y){
    return (x > y ? x : y);
}

int main(){
    int N = 0; //总钱数 
    int m = 0; //可购买的物品个数
    cin>>N>>m;
    //N/=10;	//都是10的整数,先除以10,减少循环次数
    
    int weight[60][3] = {0}; //价格/成本/费用 以及该件所带的附件数
    int value[60][3] = {0}; //价值(价格*重要度)
    //int dp[60][32000] = {0}; //第i个物品在j容量下可以获得的最大价值
    //int i, j;
    
    for(int i = 1; i <= m; i++){ //
        int v = 0;	//该物品价格
		int p = 0;	//该物品重要度
		int q = 0;	//该物品是主件还是附件
		cin >> v >> p >> q;
		//v/=10; //
        
        if(q == 0){ //主件
            weight[i][0] = v;
            value[i][0] = v * p;
        }
        else{ //附件(绑定主件)
            if(weight[q][1] == 0){ //第一个附件绑定的主件q
                weight[q][1] = v; //
                value[q][1] = v * p; //
            }
            else{ //第二个附件绑定的主件q
                weight[q][2] = v; //
                value[q][2] = v * p; //
            }          
        }
    }
    
    //开始遍历 DP
    vector<vector<int> > dp(m + 1, vector<int>(N+1,0));//第i个物品在j容量下可以获得的最大价值
    for(int i = 1; i <= m; i++){ //
        for(int j = 1; j <= N; j++){ //
            //假如不放入第i个物品
            dp[i][j] = dp[i - 1][j]; //一定要单独列出来
            
            //假如放入第i个物品
            if(j >= weight[i][0]){ //可以容下第i个主件时,比较放第i个或者不放第i个物品的价值
                dp[i][j] = getMax(dp[i][j], dp[i - 1][j - weight[i][0]] + value[i][0]);
            }
            if(j >= weight[i][0] + weight[i][1]){ //可以容下第i个主件和此主件的第1个附件时
                dp[i][j] = getMax(dp[i][j], dp[i - 1][j - weight[i][0] - weight[i][1]] + value[i][0] + value[i][1]);
            }
            if(j >= weight[i][0] + weight[i][2]){ //可以容下第i个主件和此主件的第2个附件时
                dp[i][j] = getMax(dp[i][j], dp[i - 1][j - weight[i][0] - weight[i][2]] + value[i][0] + value[i][2]);
            }
            if(j >= weight[i][0] + weight[i][1] + weight[i][2]){ //可以容下第i个主件和此主件的第1个附件和第2个附件时
                dp[i][j] = getMax(dp[i][j], dp[i - 1][j - weight[i][0] - weight[i][1] - weight[i][2]] + value[i][0] + value[i][1] + value[i][2]);
            }
        }
    }
    
    cout << dp[m][N]/* * 10*/ << endl;
    
    return 0;
}


17.坐标移动

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

using namespace std;

unordered_map<char, pair<int, int>> m = {
    {'A', make_pair(-1, 0)},
    {'W', make_pair(0, 1)},
    {'S', make_pair(0, -1)},
    {'D', make_pair(1, 0)}
};

void process(string str, pair<int, int>& res/*int x, int y*/){
    int divnum = 0;
    for(char c : str){
        if(c == ';') divnum++;
    }
    
    string s = "";
    stringstream iss(str);
    while(divnum--){
        getline(iss, s, ';');
        //cout<<s<<endl; 
        if(s[0] == 'A' || s[0] == 'W' || s[0] == 'S' || s[0] == 'D'){
            string tmp = s.substr(1);
            int num = 0;
            for(char c : tmp){
                if(c >= 'A' && c <= 'Z'){
                    num = 0;
                    break;
                }
                else{
                    num = num * 10 + (c - '0');
                }
            }        
            //cout << num << endl;
            
            res.first += m[s[0]].first * num;
            res.second += m[s[0]].second * num;
        }  
        
    }
    
    return;
}

int main(){
    string str = "";
    getline(cin, str);
    
    //int x = 0, y = 0;
    pair<int, int> res({0, 0});
    process(str, res);
    
    cout << res.first << "," << res.second << endl;
    
    return 0;
}

18.识别有效的IP地址和掩码并进行分类统计

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

using namespace std;

//判断ip
/*判断IP地址是否合法,如果满足下列条件之一即为非法地址
数字段数不为4
存在空段,即【192..1.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){
            return false;
        }
    }
    return count == 4;
}

//判断是否私有
bool isPrivate(string s){
    stringstream iss(s);
    string tmp = "";
    vector<int> vec;
    while(getline(iss, tmp, '.')){
        vec.push_back(stoi(tmp));
    }
    
    if(vec[0] == 10) return true;
    if(vec[0] == 172 && (vec[1] >= 16 && vec[1] <= 31)) return true;
    if(vec[0] == 192 && vec[1] == 168) return true;
    return false;
}

//判断掩码
/*
判断子网掩码是否合法,如果满足下列条件之一即为非法掩码
不是一个合格的IP地址
在二进制下,不满足前面连续是1,然后全是0
在二进制下,全为0或全为1

如何判断一个掩码地址是不是满足前面连续是1,然后全是0?
将掩码地址转换为32位无符号整型,假设这个数为b。如果此时b为0,则为非法掩码
将b按位取反后+1。如果此时b为1,则b原来是二进制全1,非法掩码
如果b和b-1做按位与运算后为0,则说明是合法掩码,否则为非法掩码
*/
bool judgeMask(string s){
    stringstream iss(s);
    string tmp = "";
    unsigned int val = 0;
    while(getline(iss, tmp, '.')){
        val = (val << 8) + stoi(tmp); //拿到子网掩码的32位无符号整形值  
    }
    //cout << "val" << val << endl;
    if(val == 0) return false; //全为0
    if(~val + 1 == 1) return false; //全为1
    if((val | (val - 1)) == 0xFFFFFFFF) return true; //全面连续1,后面连续0
    //if((val & (val - 1)) == 0) return true;
    return false;
}

int main(){
    string str = "";
    int a = 0, b = 0, c = 0, d = 0, e = 0, err = 0, p = 0;
    while(cin >> str){
        stringstream iss(str);
        string tmp = "";
        vector<string> vec;
        while(getline(iss, tmp, '~')){
            vec.push_back(tmp);//按行读取输入,根据字符‘~’ 将IP地址与子网掩码分开
        }
        //cout<<vec[0]<<" "<<vec[1]<<endl;
        int first = stoi(vec[0].substr(0, vec[0].find_first_of('.')));
        //查看子网掩码是否合法
        if((first != 127) && (!judgeIP(vec[0]) || !judgeMask(vec[1]))) err++; //多加first != 127的判断(否则会多加一次err)
        else{//合法,则继续检查IP地址          
            //查看IP地址是否合法
            if(!judgeIP(vec[0])) err++;//非法,相应统计项+1
            else{//合法,查看IP地址属于哪一类,是否是私有ip地址;相应统计项+1
                if(isPrivate(vec[0])) p++;
                             
                if(first == 0 || first == 127) continue;
                else if(first >= 1 && first <= 126) a++;
                else if(first >= 128 && first <= 191) b++;
                else if(first >= 192 && first <= 223) c++;
                else if(first >= 224 && first <= 239) d++;
                else if(first >= 240 && first <= 255) e++;
            }         
        }
    }
    
    cout << a << " " << b << " " << c << " " << d << " " << e << " " << err << " " << p << endl;
    
    return 0;
}

19.简单错误记录

#include <bits/stdc++.h>

using namespace std;
//只用输出最后出现的八条错误记录——考虑队列,超过八条时弹出 (双端队列,从后端进,从前端出)
//对相同的错误记录只记录一条,但是错误计数增加——考虑哈希表,键为错误记录,值为错误计数


int main(){
    string str = "";
    unordered_map<string, int> mp;
    deque<string> dq;
    
    while(getline(cin, str)){
        str = str.substr(str.find_last_of('\\') + 1); 
        //cout << str << endl; //cqzlyaszjvlsjmkwoqijggmybr 645
        int spacePos = str.find_last_of(' '); //从0开始的第26个索引处是空格
        //cout << spacePos << endl; //26
        if(spacePos > 16){
            str = str.substr(spacePos - 16);
            //cout << str << endl; //lsjmkwoqijggmybr 645
        }
        
        if(mp.find(str) == mp.end()){
            dq.push_back(str);
        }
        mp[str]++;
        
        //cout << dq.front() << endl; //lsjmkwoqijggmybr 645
        if(dq.size() > 8) dq.pop_front(); //超过八条则 从前面推出
    }
    
    for(auto s : dq){
        cout << s << " " << mp[s] << endl;
    }
    
    return 0;
}

20.密码验证合格程序

#include <bits/stdc++.h>

using namespace std;

//包括大小写字母.数字.其它符号,以上四种至少三种
bool checkChar(string str){
    int i = 0, j = 0, k = 0, l = 0;
    for(char c : str){
        if(isupper(c)){
            i = 1;
        }
        else if(islower(c)){
            j = 1;
        }
        else if(isdigit(c)){
            k = 1;
        }
        else{
            l = 1;
        }
    }
    
    if(i + j + k + l >= 3){
        return true;
    }
    
    return false;
}

//不能有长度大于2的包含公共元素的子串重复 
bool check(string str){
    for(int i = 0; i < str.size() - 3; i++){
        string s = str.substr(i, 3);
        if(str.find(s, i + 3) != str.npos){ //从第i + 3位开始找s
            return false;
        }
    }
    return true;
}

int main(){
    string str = "";
    while(cin >> str){
        if(str.size() > 8 && checkChar(str) && check(str)){
            cout << "OK"<<endl;
        }
        else{
            cout << "NG"<<endl;
        }
    }
    
    return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wrdoct

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值