牛客网——华为机试题库(11-20)

11、数字颠倒

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    string str;
    while(n)
    {
        str += (n % 10) + '0';
        n /= 10;
    }
    
    cout << str << endl;
    
    return 0;
}

12、字符串反转

# include <iostream>
# include <string>

using namespace std;

int main()
{
    string str;
    while(getline(cin, str))
    {
        for(int i = str.size() - 1; i >= 0; i--)
            cout << str[i];
        cout << endl;
    }
    return 0;
}
#include <iostream>
#include <algorithm>

using namespace std;

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

13、句子逆序

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    string str;
    while(getline(cin, str))
    {
        reverse(str.begin(), str.end());
        for(int i = 0; i < str.size(); ++i)
        {
            int j = i;
            while(j < str.size() && str[j] != ' ')
            {
                ++j;
            }
            reverse(str.begin() + i, str.begin() +j);
            i = j;
        }
        cout << str << endl;
    }
    
    return 0;
}

14、字串的连接最长路径查找

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<string> dic;
    while(n--)
    {
        string str;
        cin >> str;
        dic.push_back(str);
    }
    sort(dic.begin(), dic.end());
    
    for(auto x : dic)
        cout << x << endl;
    
    return 0;
}

15、求int型数据在内存中存储时1的个数

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int count = 0;
    while(n)
    {
        if(n & 1)
            count ++;
        n = n >> 1;
    }
    cout << count << endl;
    
    return 0;
}
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int num = 0;
    while(n)
    {
        if(n % 2 == 1)
            num++;
        n /= 2;
    }
    
    cout << num << endl;
    
    return 0;
}

16、购物单

#include <iostream>
#include <vector>

using namespace std;
//总钱数小于32000
int dp[32000];

int main()
{
    int N, m;
    cin >> N >> m;
    
    //定义几个数组
    vector<int> zj(m+1), zjvw(m+1), fj1(m+1), fj1vw(m+1), fj2(m+1), fj2vw(m+1);
    
    for(int i = 1; i <= m; ++i)
    {
        int v, p, q;
        cin >> v >> p >> q;
        //主件
        if(q == 0)
        {
            zj[i] = v;
            zjvw[i] = v * p;
        }
        //附件1
        //不是i
        //q值对应的是从属的主件
        else if(fj1[q] == 0)
        {
            fj1[q] = v;
            fj1vw[q] = v * p;
        }
        //附件2
        else if(fj2[q] == 0)
        {
            fj2[q] = v;
            fj2vw[q] = v * p;
        }
    }
    
    //动态规划
    for(int i = 1; i <= m; ++i)
    {
        for(int j = N; j >= 1; j--)
        {
            //只要主件
            if(j >= zj[i])
                dp[j] = max(dp[j], dp[j-zj[i]] + zjvw[i]);
            //主件+附件1
            if(j >= zj[i] + fj1[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj1[i]] + zjvw[i] +fj1vw[i]);
            //主件+附件2
            if(j >= zj[i] + fj2[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj2[i]] + zjvw[i] +fj2vw[i]);
            //主件+附件1+附件2
            if(j >= zj[i] + fj1[i] + fj2[i])
                dp[j] = max(dp[j], dp[j-zj[i]-fj1[i]-fj2[i]] + zjvw[i] +fj1vw[i] +fj2vw[i]);
        }
    }
    cout << dp[N] << endl;
    
    return 0;
}

17、坐标移动

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    string str;
    
    while(cin >> str)
    {
        //初始化坐标
        int x = 0, y = 0;
        //存储单步操作
        vector<string> steps;
        
        //把字符串拆分
        int wordlen = 0;
        for(int i = 0; i < str.size(); ++i)
        {
            while(str[i] != ';')
                wordlen ++, i ++;
            steps.push_back(str.substr(i - wordlen, wordlen));
            wordlen = 0;
        }
        //分解成功
        //for(auto x : steps) cout << x << endl;
        
        //对单个steps执行坐标变换
        for(int i = 0; i < steps.size(); ++i)
        {
            int num = 0;
            //长度3  A10
            if(steps[i].length() == 3 && steps[i][1] <= '9' && steps[i][1] >= '0' &&  steps[i][2] <= '9' && steps[i][2] >= '0')
                num = (steps[i][1] - '0') * 10 + steps[i][2] - '0';
            //长度2  A5
            if(steps[i].length() == 2 && steps[i][1] <= '9' && steps[i][1] >= '0')
                num = steps[i][1] - '0';
            
            switch(steps[i][0])//ASDW
            {
                case 'A': x -= num; break;
                case 'D': x += num; break;
                case 'W': y += num; break;
                case 'S': y -= num; break;
                default: break;
            }
        }
        cout << x << ',' << y << endl;
    }
    
    return 0;
}

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

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

using namespace std;

int strtoint(string str)
{
    //68
    int sum = 0;
    for(int i = 0; i < str.size(); i ++)
        sum = sum * 10 + str[i] - '0';
    return sum;
}

vector<int> toint(string str)
{
    vector<int> res;
    //10.70.44.68
    int len = 0;
    for(int i = 0; i < str.size(); i ++)
    {
        while(str[i] != '.' && i < str.size())
            i ++, len ++;
        string nums = str.substr(i - len, len);
        int num = strtoint(nums);
        res.push_back(num);
        len = 0;
    }
    return res;
}

bool ismask(vector<int> mask)
{
    //255 254 255 0
    //长度 == 4
    //1在前
    if(mask.size() != 4)
        return false;
    
    if(mask[0] == 255)
    {
        if(mask[1] == 255)
        {
            if(mask[2] == 255)
            {
                //8位2进制
                //7个1  6个1 .。。 1个1  + 000
                if(mask[3] == 254 || mask[3] == 252 || mask[3] == 248 || mask[3] == 240 || mask[3] == 224 || mask[3] == 192 || mask[3] == 128 || mask[3] == 0)
                    return true;
                else
                    return false;
            }
            else{
                if(mask[2] == 254 || mask[2] == 252 || mask[2] == 248 || mask[2] == 240 || mask[2] == 224 || mask[2] == 192 || mask[2] == 128 || mask[2] == 0)
                    if(mask[3] == 0)
                        return true;
                    else
                        return false;
            }
        }
        else{
            if(mask[1] == 254 || mask[1] == 252 || mask[1] == 248 || mask[1] == 240 || mask[1] == 224 || mask[1] == 192 || mask[1] == 128 || mask[1] == 0)
                if(mask[3] == 0 && mask[2] == 0)
                    return true;
                else
                    return false;
            
        }
    }
    else{
        if(mask[0] == 254 || mask[0] == 252 || mask[0] == 248 || mask[0] == 240 || mask[0] == 224 || mask[0] == 192 || mask[0] == 128)
            if(mask[1] == 0 && mask[2] == 2 && mask[3] == 0)
                return true;
            else
                return false;
    }
    return false;
}

int main()
{
    vector<int> res(7,0);
    string str;
    while(cin >> str)
    {
        //10.70.44.68~255.254.255.0
        //分割ip mask
        string ips, masks;
        vector<string> temp;
        int len = 0;
        for(int i = 0; i < str.size(); i ++)
        {
            while(str[i] != '~' && i < str.size())
                i ++, len ++;
            string s = str.substr(i - len, len);
            temp.push_back(s);
            len = 0;
        }
        ips = temp[0];
        masks = temp[1];
        //cout << ips << ' ' << masks << endl;//分割成功
        
        //字符串 二进制 比较大小 转换成int
        vector<int> ip, mask;
        ip = toint(ips);
        mask = toint(masks);
        
        //子网掩码 11111 0000
        if(ismask(mask))
        {
            //判断ip
            
            //A类地址1.0.0.0~126.255.255.255;
            //B类地址128.0.0.0~191.255.255.255;
            //C类地址192.0.0.0~223.255.255.255;
            //D类地址224.0.0.0~239.255.255.255;
            //E类地址240.0.0.0~255.255.255.255
            //私网IP范围是
            //10.0.0.0~10.255.255.255
            //172.16.0.0~172.31.255.255
            //192.168.0.0~192.168.255.255
            if(ip[1] >= 0 && ip[1] <= 255 && ip[2] >= 0 && ip[2] <= 255 && ip[3] >= 0 && ip[3]<+ 255)
            {
                if(ip[0] >= 1 && ip[0] <=126)//A
                {
                    res[0] ++;
                    if(ip[0] == 10)//私有
                        res[6]++;
                }
                else if(ip[0] >= 128 && ip[0] <= 191)//b
                {
                    res[1] ++;
                    if(ip[0] == 172 && ip[1] >= 16 && ip[1] <= 31)//私有
                        res[6]++;
                }
                else if(ip[0] >= 192 && ip[0] <= 223)//c
                {
                    res[2] ++;
                    if(ip[0] == 192 && ip[1] == 168)//私有
                        res[6]++;
                }
                else if(ip[0] >= 224 && ip[0] <= 239)//d
                    res[3] ++;
                else if(ip[0] >= 240 && ip[0] <= 255)//e
                    res[4] ++;
            }
        }
        else
            res[5] ++;
    }
    
    //A~E  0~4
    //错误 5
    //私有 6
    cout << res[0] << ' ' << res[1] << ' ' << res[2] << ' ' << res[3] << ' ' << res[4] << ' ' << res[5] << ' '<< res[6] << endl;
    return 0;
}

19、简单错误记录

#include <iostream>
#include <string.h>
using namespace std;

//文件名 行数 次数
struct record
{
    char s[100];
    int col;
    int count;
};

int main()
{
    char filename[100];
    int col;
    int cnt = 0;//总记录条数 8
    
    record rec[10000];
    while(cin >> filename >> col)
    {
        //分隔 /路径
        //E:\V1R2\product\fpgadrive.c   1325
        //fpgadrive.c
        char *p = strrchr(filename, '\\');
        p++;
        if(strlen(p) > 16)//截取后16位
            p = p + (strlen(p) - 16);
        
        int flag = 0;
        
        //查重
        for(int i = 0; i < cnt; ++i)
        {
            if(strcmp(rec[i].s, p) == 0 && rec[i].col == col)
            {
                rec[i].count ++;
                flag = 1;
                break;
            }
        }
        
        if(!flag)//没有当前错误记录,全部插进去
        {
            strcpy(rec[cnt].s, p);
            rec[cnt].col = col;
            rec[cnt].count = 1;
            cnt ++;
        }
    }
    
    //输出8条
    int i = 0;
    if(cnt > 8)
        i = cnt - 8;
    else i = 0;
    
    for(; i < cnt; ++i)
    {
        cout << rec[i].s << " " << rec[i].col << " " << rec[i].count << endl;
        
    }
    return 0;
}

20、密码验证合格程序

#include <iostream> 
#include <vector>
#include <string.h>

using namespace std;

int main()
{
    vector<string> res;//OK NG
    string str;
    
    while(cin >> str)
    {
        int num[4] = {0};
        //1.长度超过8位
        if(str.size() <= 8)
            res.push_back("NG");
        else
        {
            //2.包括大小写字母.数字.其它符号,以上四种至少三种
            for(auto it = str.begin(); it != str.end(); it++)
            {
                if((*it) >= 'A' && (*it) <= 'Z') num[0] = 1;
                else if((*it) >= 'a' && (*it) <= 'z') num[1] = 1;
                else if((*it) >= '0' && (*it) <= '9') num[2] = 1;
                else num[3] = 1;
            }
            
            int count = 0;
            for(int i = 0; i < 4; i++)
            {
                count += num[i];
            }
            if(count < 3)
                res.push_back("NG");
            else
            {
                //3.不能有相同长度大于等于2的子串重复
                //查找字符串a是否包含子串b,不是用strA.find(strB) > 0
                //而是strA.find(strB) != string :: npos;
                //其中string :: npos是个特殊值,说明查找没有匹配
                //string::size_type pos = strA.find(strB);
                string::size_type pos;
                for(int i = 0; i < str.size(); i++)
                {
                    string sub = str.substr(i, 3);
                    pos = str.find(sub, i + 3);
                    if(pos != string::npos)
                        break;
                }
                if(pos == string::npos)
                    res.push_back("OK");
                else
                    res.push_back("NG");
                
            }
        }
    }
   
    for(auto x : res)
        cout << x << endl;
    
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值