华为笔试题库之较难--难度

题记:若立志投身算法研究,可精研理论算法:动态规划、递归、深度搜索等;若以解决问题为目的,主要为了工作内容,当尝试快而简单的方法,这该是学习的本意。

1.明明的随机数

在这里插入图片描述
在这里插入图片描述

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
int main()
{
    int n;
    int a;
    //以数组下标来存储随机数,下标对应的数组值为1,来说明是否是存储的随机数
    while(~scanf("%d", &n))
    {
        int count[1001] = {0};
		int i;
        for (i = 0; i < n; i++)
        {
            scanf("%d", &a);
            count[a] = 1;
        }
        for (i = 0; i < 1001; i++)
        {
            if (count[i] == 1)
            {
                printf("%d\n", i);
            }
        }
    }
    return 0;
}
//容器实现:
#include<iostream>
#include<set>
using namespace std;
int main() {
    int n;
    while(cin>>n) {    //输入每组数据的个数
        set<int> order;    //使用set容器可以自动实现去重和排序的操作,这里很关键
        for(int i=0;i<n;i++) {
            int num;
            cin>>num;    //输入数字
            order.insert(num);    //插入到容器order中
        }
        set<int>::iterator it;    //set类型迭代器
        for(it=order.begin();it!=order.end();it++){
            cout<<*it<<endl;    //遍历输出
        }
    }
    return 0;
}

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

在这里插入图片描述
在这里插入图片描述

一些需要注意的细节
1.类似于【0...】和【127...】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时可以忽略
2.私有IP地址和A,B,C,D,E类地址是不冲突的,也就是说需要同时+1
3.如果子网掩码是非法的,则不再需要查看IP地址
4.全零【0.0.0.0】或者全一【255.255.255.255】的子网掩码也是非法的
在这里插入图片描述

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

bool judge_ip(string ip){
    int j = 0;
    istringstream iss(ip);
    string seg;
    while(getline(iss,seg,'.'))
        if(++j > 4 || seg.empty() || stoi(seg) > 255)
            return false;
    return j == 4;
}

bool is_private(string ip){
    istringstream iss(ip);
    string seg;
    vector<int> v;
    while(getline(iss,seg,'.')) v.push_back(stoi(seg));
    if(v[0] == 10) return true;
    if(v[0] == 172 && (v[1] >= 16 && v[1] <= 31)) return true;
    if(v[0] == 192 && v[1] == 168) return true;
    return false;
}

bool is_mask(string ip){
    istringstream iss(ip);
    string seg;
    unsigned b = 0;
    while(getline(iss,seg,'.')) b = (b << 8) + stoi(seg);
    if(!b) return false;
    b = ~b + 1;
    if(b == 1) return false;
    if((b & (b-1)) == 0) return true;
    return false;
}

int main(){
    string input;
    int a = 0,b = 0,c = 0,d = 0,e = 0,err = 0,p = 0;
    while(cin >> input){
        istringstream is(input);
        string add;
        vector<string> v;
        while(getline(is,add,'~')) v.push_back(add);
        if(!judge_ip(v[1]) || !is_mask(v[1])) err++;
        else{
            if(!judge_ip(v[0])) err++;
            else{
                int first = stoi(v[0].substr(0,v[0].find_first_of('.')));
                if(is_private(v[0])) p++;
                if(first > 0 && first <127) a++;
                else if(first > 127 && first <192) b++;
                else if(first > 191 && first <224) c++;
                else if(first > 223 && first <240) d++;
                else if(first > 239 && first <256) e++;
            }
        }
    }
    cout << a << " " << b << " " << c << " " << d << " " << e << " " << err << " " << p << endl;
    return 0;
}

3.简单的错误记录

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <string>
#include <deque>
#include <map>
using namespace std;

int main() {
    string str;
    map<string, int> result;
    deque<string> deq;
    while (getline(cin, str)) {
        str = str.substr(str.find_last_of('\\') + 1);
        int pos = str.find_last_of(' ');
        if ( pos > 16) {
            str = str.substr(pos - 16);
        }

        if (result.find(str) == result.end()) deq.push_back(str);
        ++result[str];
        if (deq.size() > 8) deq.pop_front();
    }
    for (auto x : deq) {
        cout << x << " " << result[x] << endl;
    }
    return 0;
}

4.数据分类处理

在这里插入图片描述
在这里插入图片描述
示例1

输入:
15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
5 6 3 6 3 0
输出:
30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786
说明:
将序列R:5,6,3,6,3,0(第一个5表明后续有5个整数)排序去重后,可得0,3,6。
序列I没有包含0的元素。
序列I中包含3的元素有:I[0]的值为123、I[3]的值为453、I[7]的值为3、I[9]的值为453456、I[13]的值为453、I[14]的值为123。
序列I中包含6的元素有:I[1]的值为456、I[2]的值为786、I[4]的值为46、I[8]的值为665、I[9]的值为453456、I[11]的值为456、I[12]的值为786。
最后按题目要求的格式进行输出即可。

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

bool IsContain(const int &Ri,const int &I)//判断是否含有字符串,string.find
{
    //把I变成字符串,C11新特性
    string str_I = to_string(I);
    string str_R = to_string(Ri);
    if(int(str_I.find(str_R)) != -1 )
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int numI,temI,numR,temR;
    while(cin>>numI)
    {
        //读取并存储
        vector<int> vecI,vecR;//用于存储读进来的数据
        for(int i = 0;i<numI;i++)
        {
            cin>>temI;
            vecI.push_back(temI);
        }
        cin>>numR;
        for(int i = 0;i<numR;i++)
        {
            cin>>temR;
            vecR.push_back(temR);
        }
        //对R排序去重
        sort(vecR.begin(),vecR.end());//排序
        vecR.erase(unique(vecR.begin(),vecR.end()),vecR.end());//unique 不改变长度,需要搭配erase

        //循环I,R进行挑选
        vector<int>::iterator pr_I;//定义两个迭代器
        vector<int>::iterator pr_R;
        vector<vector<int>> sum_index;//用于存储所有挑选出来的索引
        vector<int> tem_index;//暂时存储对应Ri挑选出来的I索引
        vector<int> sum_Ri;//计算总的整数个数
        int sum_num = 0;
        for(pr_R = vecR.begin();pr_R != vecR.end();++pr_R)//循环R
        {
    //         cout<<*pr_R<<endl;
            int i = 0;
            for(pr_I = vecI.begin();pr_I != vecI.end();++pr_I)//循环I
            {
                if(IsContain(*pr_R,*pr_I))// !=0
                {
                    tem_index.push_back(i);
                }
                i++;
            }
            if(tem_index.size() != 0)
            {
                sum_index.push_back(tem_index);
                sum_Ri.push_back(*pr_R);
                sum_num += tem_index.size()*2 +2;
    //             cout<<*pr_R<<" "<<tem_index.size()<<endl;
            }
            tem_index.clear();
        }
        //输出
        cout<<sum_num<<" ";
        for(int i=0;i<sum_index.size();i++)
        {
            cout<<sum_Ri[i]<<" "<<sum_index[i].size()<<" ";
            for(int j = 0;j<sum_index[i].size();j++)
            {
                cout<<sum_index[i][j]<<" "<<vecI.at(sum_index[i][j])<<" ";
            }
        }
        cout<<endl;
    }
    return 0;
}

5.字符串合并处理

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <algorithm>
using namespace std;
//字符串合并处理的函数接口
void Process_String(string str1, string str2, string strOutput)
{
    //字典法:只考虑 '0' 到 '9' ,'a' 到 'f','A' 到 'F' 的字符即可,其余字符不做改变,照原输出
    char Intput[] = {"0123456789abcdefABCDEF"}; //输入参照字典(数字 + 大小写字母)
//    int Output[] = "084c2a6e195d3b7f5d3b7f"; //输出参照字典(小写)
    char Output[] = {"084C2A6E195D3B7F5D3B7F"}; //输出参照字典(数字 + 大写字母)
    strOutput = str1 + str2; //合并两个字符串
    string odd_str; //下标为奇数的字符组成的字符串,奇数位字符串
    string even_str; //下标为偶数的字符串组成的字符串,偶数位字符串
    //根据字符在合并字符串中的次序,按字典序分奇数位、偶数位独立来排,但次序的奇偶性不变,即原来是奇数位,排序后还是奇数位
    for (int i = 0; i < strOutput.size(); i++)
    {
        if (i % 2 == 0)
        {
            odd_str += strOutput[i];
        }
        else if (i % 2 == 1)
        {
            even_str += strOutput[i];
        }
    }
    sort(odd_str.begin(), odd_str.end()); //奇排序
    sort(even_str.begin(), even_str.end()); //偶排序
    //将按奇数位、偶数位排序后的字符再填回合并字符串 strOutput
    int j = 0; //奇数位字符串的下标
    int k = 0; //偶数位字符串的下标
    for (int i = 0; i < strOutput.size(); i++)
    {
        if (i % 2 == 0)
        {
            strOutput[i] = odd_str[j];
            j++;
        }
        else if (i % 2 == 1)
        {
            strOutput[i] = even_str[k];
            k++;
        }
    }
    //对字符(符合字典 Input[])所代表的 16 进制的数进行 BIT 倒序的操作,并转换为相应的大写字符
    for (int i = 0; i < strOutput.size(); i++)
    {
        if ((strOutput[i] >= '0') && (strOutput[i] <= '9'))
        {
            strOutput[i] = Output[strOutput[i] - '0'];
        }
        else if ((strOutput[i] >= 'a') && (strOutput[i] <= 'f'))
        {
            strOutput[i] = Output[strOutput[i] - 'a' + 10];
        }
        else if ((strOutput[i] >= 'A') && (strOutput[i] <= 'F'))
        {
            strOutput[i] = Output[strOutput[i] - 'A' + 16];
        }
    }
    cout << strOutput << endl;
    return;
}
//主函数
int main()
{
    string str1, str2, strOutput;
    while (cin >> str1 >>str2)
    {
        Process_String(str1, str2, strOutput);
    }
    return 0;
}

6.学英语

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

void getResult(vector<string> &result, int &num);

int main() {
    long num;
    cin >> num;
    
    vector<string> result;
    
    int numMillion = num / 1000000;
    if (numMillion > 0) {
        getResult(result, numMillion);
        result.push_back("million");
    }
    
    int numThousand = num % 1000000 / 1000;
    if (numThousand > 0) {
        getResult(result, numThousand);
        result.push_back("thousand");
    }
    
    int numOne = num % 1000000 % 1000;
    if (numOne > 0) {
        getResult(result, numOne);
    }
    
    for (auto s : result) {
        cout << s << " ";
    }
    cout << endl;
    
    return 0;
}

void getResult(vector<string> &result, int &num) {
    map<int, string> u10 = {
        {1, "one"},
        {2, "two"},
        {3, "three"},
        {4, "four"},
        {5, "five"},
        {6, "six"},
        {7, "seven"},
        {8, "eight"},
        {9, "nine"}
    };
    map<int, string> u20 = {
        {10, "ten"},
        {11, "eleven"},
        {12, "twelve"},
        {13, "thirteen"},
        {14, "fourteen"},
        {15, "fifteen"},
        {16, "sixteen"},
        {17, "seventeen"},
        {18, "eighteen"},
        {19, "nineteen"}
    };
    map<int, string> u100 = {
        {2, "twenty"},
        {3, "thirty"},
        {4, "forty"},
        {5, "fifty"},
        {6, "sixty"},
        {7, "seventy"},
        {8, "eighty"},
        {9, "ninety"}
    };
    
    bool hundredSign = false;
    bool tenSign = false;
    if (num >= 100) {
        hundredSign = true;
        result.push_back(u10[num / 100]);
        result.push_back("hundred");
        num %= 100;
    }
    if (num >= 10) {
        tenSign = true;
        if (hundredSign) {
            result.push_back("and");
        }
        if (num <= 19) {
            result.push_back(u20[num]);
            num = 0;
        }
        else {
            result.push_back(u100[num / 10]);
            num %= 10;
        }
    }
    if (num > 0) {
        if (hundredSign && !tenSign) {
            result.push_back("and");
        }
        result.push_back(u10[num]);
    }
}

7.成绩排序

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,flag;
    while(cin>>n>>flag)
    {
        vector<pair<string,int>> res(n);
        for(int i=0;i<n;i++)
        {
        cin>>res[i].first>>res[i].second;
        }
        if(flag==0)
        {
            stable_sort(res.begin(),res.end(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second>b.second;
            });
        }
        else if(flag==1)
        {
            stable_sort(res.begin(),res.end(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second<b.second;
            });
        }
        for(int i=0;i<n;i++)
            cout<<res.at(i).first<<" "<<res.at(i).second<<endl;
    }
    return 0;
}

//方案二:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,flag;
    while(cin>>n>>flag)
    {
        vector<pair<string,int>> res(n);
        for(int i=0;i<n;i++)
        {
        cin>>res[i].first>>res[i].second;
        }
        if(flag==0)
        {
            stable_sort(res.begin(),res.end(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second>b.second;
            });
        }
        else if(flag==1)
        {
            stable_sort(res.begin(),res.end(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second<b.second;
            });
        }
        for(int i=0;i<n;i++)
            cout<<res.at(i).first<<" "<<res.at(i).second<<endl;
    }
    return 0;
}

8.数组分组

在这里插入图片描述

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
int des5_3=0;
vector<int>vec;
bool dfs(int i,int des)
{
    if(i==vec.size())
    {
        return abs(des)==des5_3;
    }
    else
    {
        return dfs(i+1,des+vec[i])||dfs(i+1,des-vec[i]);
    }
}
int main()
{
    int c;
    while(cin>>c)
    {
        vec.clear();
        int sum3=0;
        int sum5=0;
        while(c--)
        {
            int data;
            cin>>data;
            if(data%3==0)
            {
                sum3+=data;
            }
            else if(data%5==0)
            {
                sum5+=data;
            }
            else
            {
               vec.push_back(data); 
            }
        }
        des5_3=abs(sum5-sum3);
        if(dfs(0,0))
        {
            cout<<"true"<<endl;
        }
        else
        {
            cout<<"false"<<endl;
        }
    }
    return 0;
}

9.人民币转换

在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;

vector<string> single = {"零", "壹", "贰", "叁", "肆",
                         "伍", "陆", "柒", "捌", "玖"};

void sayPre(string &pre) {
    if (pre == "0") return ;
    // 判断小数点前面是不是空的
    for (int i = 0, j = pre.size() - 1; i < pre.size(); i++, j--) {
        // i代表的是我们遍历的字符串, j是我们i后面有几个数
        if (pre[i] != '0' and not(pre[i] == '1' and j % 4 == 1))
            cout << single[pre[i] - '0'];
        // 转换中文
        if (j != 0 and j % 8 == 0 and j >= 8) 
            cout << "亿";
        if (j != 0 and j % 4 == 0 and j % 8 != 0)
            pre[i + 1] == '0' ? cout << "万零" : cout << "万";
        if (j != 0 and j % 4 == 3 and pre[i] != '0') 
            pre[i + 1] == '0' and pre[i + 2] != '0' ? cout << "仟零" : cout << "仟";
        if (j != 0 and j % 4 == 2 and pre[i] != '0')
            pre[i + 1] == '0' and pre[i + 2] != '0' ? cout << "佰零" : cout << "佰";
        if (j != 0 and j % 4 == 1 and pre[i] != '0')
            cout << "拾";
    }
    // 上面的if分别对应后面输出的亿万千百十
    cout << "元";
    // 最后我们输出元
}

void sayEnd(string &end) {
    if (end == "00")
        cout << "整\n";
    else if (end[0] == '0')
        cout << single[end[1] - '0'] << "分\n";
    else if (end[1] == '0')
        cout << single[end[0] - '0'] << "角\n";
    else
        cout << single[end[0] - '0'] << "角" << single[end[1] - '0'] << "分\n";
    // 分类讨论, 讨论我们小数点后两位的所有情况
}

signed main() {
    string s;
    while (cin >> s) {
        string pre = "", end = "";
        bool okk = false;
        for (auto &it : s) {
            if (it == '.') {
                okk = true;
                continue;
            }
            okk ? end += it : pre += it;
        }
        // 这里我们以小数点为分隔, 把小数点前面的存储到了pre里面,
        // 小数点后面的存储到了end里面
        cout << "人民币";
        sayPre(pre), sayEnd(end);
    }
    return 0;
}

10.记负均正

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    vector<int> vec;    //容器vec存放输入的整数
    while(cin>>n) {    //将输入的整数插入到容器vec中
        vec.push_back(n);
    }
    int count = count_if(vec.begin(), vec.end(), [](int x) {return x<0;});    //count为容器vec中负数的个数
    cout<<count<<endl;    //输出负数的个数
    vector<int> pos0vec;    //容器pos0vec存放容器vec中非负的整数
    copy_if(vec.begin(), vec.end(), back_inserter(pos0vec), [](int x) {return x>=0;});    //将容器vec中非负整数拷贝到容器pos0vec中
    int length = pos0vec.size();    //length为容器pos0vec中的元素个数
    double average=0;    //平均值
    if(length>0) {
        average = accumulate(pos0vec.begin(), pos0vec.end(), 0.0);    //对容器pos0vec中的元素求和
        average /= length;    //计算平均值
    }
    cout<<fixed<<setprecision(1)<<average<<endl;    //输出平均值
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

明月醉窗台

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

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

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

打赏作者

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

抵扣说明:

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

余额充值