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

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()); //偶排序//将按奇数位、偶数位排序后的字符再填回合并字符串 strOutputint 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";elsecout << 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;
}

华为笔试题库之较难--难度相关推荐

  1. 华为校招java笔试题库_华为校招Java笔试题库,看你会不会做

    1.在java中如果声明一个类为final,表示什么意思? 答:final是最终的意思,final可用于定义变量.方法和类但含义不同,声明为final的类不能被继承. 2.父类的构造方法是否可以被子类 ...

  2. 成都农商银行软件测试面试题,农商行历年笔试真题找不到?不慌!16家农商行笔试题库等你来刷!...

    原标题:农商行历年笔试真题找不到?不慌!16家农商行笔试题库等你来刷! 今天嗖嗖还整理了农商行历年秋招笔试题型,给大家参考. 一.行测 言语理解(15题).数量关系(15题).逻辑推理(10题).思维 ...

  3. 华为机试题库+题解【C语言版】

    文章目录 前言 1.字符串最后一个单词的长度[***] 描述 输入描述 输出描述 示例 解题代码 2.计算某字符出现次数[****] 描述 输入描述 输出描述 示例 解题代码 3. 明明的随机数[** ...

  4. noi linux 试题解密,关于NOIP丨你想知道的都在这(附NOI2019笔试题库)

    提到自主招生,就不得不从最重要的比赛之一--NOIP说起. (ps:目前NOIP已改为CSP等级认证,时间.试题范围与NOIP暂无太大区别) 在NOIP(省级赛)取得省级一.二等奖在高考自主招生都会被 ...

  5. 华为笔试题---明明的随机数

    题目 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着 ...

  6. 怎么制作一个笔试题库?会计笔试题库中的题型分析?

    目前各个考试都会有对应的笔试题库供大家学习,因为现在笔试题库作为了一种产品,目前我国的笔试考试越来越多了,笔试题库也层出不穷.下面我就来介绍一下笔试题库,我们如何选择一个全面好用的笔试题库.福昕知翼有 ...

  7. IE-LAB网络实验室:,思科变革 思科笔试题库 Cisco vtp 解析

    VTP(VLAN Trunking Protocol):是VLAN中继协议,也被称为虚拟局域网干道协议.它是思科私有协议.作用是十几台交换机在企业网中,配置VLAN工作量大,可以使用VTP协议,把一台 ...

  8. 运营商计算机笔试题库及答案,计算机笔试题库及答案

    计算机的出现和逐步的普及,信息对整个社会的影响逐步提高到一种绝对重要的地位.以下是计算机笔试题库及答案,欢迎阅读. 一.选择题 1.世界上公认的第一台电子计算机诞生的年代是(  ). A)1943 B ...

  9. 【CCF】NOI笔试题库

    最新版NOI笔试题库下载 竞赛环境和竞赛规则 1.NOI 机试使用的操作系统是:Linux 2.Linux 中为文件改名使用的命令是:mv <旧文件名> <新文件名> 3.在 ...

最新文章

  1. 2018年12月,华为HCNP大面积更新题目,军哥独家解题咯
  2. python菜鸟工具-第一行Python代码之菜鸟逃离记
  3. vray for 3dmax2019中文版
  4. Py之distance:distance的简介、安装、使用方法之详细攻略
  5. 一行 Python 代码制作七夕节爱心
  6. Effective Java之在细节消息中包含能捕获失败的消息(六十三)
  7. 控制器中获取Field值
  8. apache启服务命令_Linux系统重启apache服务命令详解
  9. 植树问题python_《程序员的数学》思考题(一)
  10. 大厂都搞不定的安全难题,被这家初创公司破解了!
  11. .rgb格式文件的Python读取、格式转换
  12. 批处理创建桌面快捷方式
  13. 工业机器人实训耗材_工业机器人实训课程
  14. win7计算机的用户名和密码,win7文件共享访问需要输入用户名和密码如何解决
  15. IP-guard功能模块简介
  16. Web报表系统葡萄城报表:报表设计
  17. SAP ABAP BDC基础使用方法
  18. Word文档进行XXE攻击
  19. AutoCad 新建或打开对话框 经常变为命令提示行的解决办法!
  20. 98% after emitting CopyPlugin vue启动停止的原因

热门文章

  1. python能处理nc文件吗_利用python如何处理nc数据详解
  2. Linux HugePage
  3. 英语口语学习推荐的21部电影
  4. 【转】高手速成android开源项目【View篇】
  5. WordPress快速增加百度收录,加快网站内容抓取
  6. 从技术小白到收获BAT研发offer,分享我的学习经验和感悟(赠送相关学习资料)
  7. Windows server 2019 安装VPN
  8. 使用 CSS Color-Mix() 简化你的调色板
  9. 实现一周之内自动登录的 cookie和session还有localStorage的存储机制
  10. nokia5200中使用ucweb浏览器