- 华为笔试练习
- 字符串
- HJ4 字符串分隔
- HJ17 坐标移动
- HJ18 识别有效的IP地址和掩码并进行分类统计
- HJ20 密码验证合格程序
- HJ21 简单密码
- HJ39 判断两个IP是否属于同一子网
- HJ58 输入n个整数,输出其中最小的k个
- HJ65 查找两个字符串a,b中的最长公共子串
- HJ105 记负均正II
- HJ31 单词倒排
- 二叉树
- 144 二叉树的前序遍历
- 104. 二叉树的最大深度
- 列表
- HJ48 从单向链表中删除指定值的节点
- HJ53 杨辉三角的变形
- HJ68 成绩排序
- HJ69 矩阵乘法
- 数字与数学思维
- HJ5 进制转换
- HJ6 质数因子
字符串 HJ4 字符串分隔
#include#include //涉及到字符串 using namespace std; int main(void){ //创建变量,接收字符串 string str; //获取字符串的长度 //循环,循环条件:字符串不为空 while(getline(cin,str)){ int len = str.size(); //判断字符串长度是否大于等于8 while(!str.empty()){ if(len >= 8){ //若大于等于8,则打印前8位字符 cout< //若小于8,则全部输出,后面补齐0 for(int i=0;i<8-len;i++){ str +='0'; } cout< HJ17 坐标移动 #includeHJ18 识别有效的IP地址和掩码并进行分类统计#include #include using namespace std; //按步骤暴力求解 //正则也不用了 //坐标移动的函数接口 int Coordinate_Movement(string str) { int x = 0, y = 0; //初始化横纵坐标 int len = str.size(); //获取字符串的总长度 vector vec; //用一个向量来存储分割后的多个子字符串 //将字符串 str 按 ';' 分割成多个子字符串 substr,然后依次写入向量 vec 中 int sublen = 0; //记录每个子字符串的长度 for(int i = 0; i < len; i++) { if(str[i] != ';') { sublen++; continue; } vec.push_back(str.substr(i - sublen, sublen)); sublen = 0; } //坐标移动,方向(左右上下) + 大小(-99~99) for(int i = 0; i < vec.size(); i++) { //确定坐标移动的大小,[-99,99] int num = 0; //(横或纵)坐标移动的大小 //若字符串为三位有效位,则第二和第三位是坐标移动的大小 if((vec[i].size() == 3) && (vec[i][1] >='0') && (vec[i][1] <= '9') && (vec[i][2] >='0') && (vec[i][2] <= '9')) { num = (vec[i][1] - '0') * 10 + (vec[i][2] - '0'); } //若字符串为两位有效位,则第二位是坐标移动的大小 if((vec[i].size() == 2) && (vec[i][1] <= '9') && (vec[i][1] >= '0')) { num = (vec[i][1] - '0'); } //若字符串为一位有效位,则无坐标移动 if(vec[i].size() == 1) { num = 0; } //确定坐标移动的方向,左右上下 switch(vec[i][0]) { case 'A': x -= num; break; case 'D': x += num; break; case 'S': y -= num; break; case 'W': y += num; break; default: break; } } cout << x << "," << y << endl; return 0; } //主函数 int main() { string str; while (cin >> str) { Coordinate_Movement(str); } return 0; } #includeHJ20 密码验证合格程序#include #include using namespace std; //各种判断真的很恶心 int main(){ vector arr(7, 0); //分别对应题目的7个类别 string s; while(getline(cin, s)){ int n = s.length(); vector ips; //记录ip地址的数字 bool bad = false;//判断是否有误 bool isnum = false;//判断是不是数字 int num = 0; for(int i = 0; i < n; i++){ //遍历该ip字符串 if(s[i] == '.' || s[i] == '~'){ //以.或者~分割 if(isnum){ if(num > 255){ bad = true; //错误ip,数字不能超过255 break; } ips.push_back(num); isnum = false; num = 0; }else{ arr[5]++; //错误地址 bad = true; break; } }else if(s[i] >= '0' && s[i] <= '9'){ isnum = true; num = num * 10 + s[i] - '0'; //计算数字 }else{ arr[5]++; isnum = false; //错误地址,数字部分还有非数字 bad = true; break; } } if(isnum) ips.push_back(num); //最后一个数字 if(ips[0] == 0 || ips[0] == 127 || bad) continue; //忽略0或者127开头的地址,错误ip已经统计了过了,可以忽略 int mask = 4; //查看掩码的数字 while(mask < 8 && ips[mask] == 255) mask++; //找到掩码第一个不全为1的数 if(mask == 8){ //掩码全1也是不合法 arr[5]++; continue; }else if(ips[mask] == 254 || ips[mask] == 252 || ips[mask] == 248 || ips[mask] == 240 || ips[mask] == 224 || ips[mask] == 191 || ips[mask] == 128) mask++; //各类掩码含1的最后一位 while(mask < 8 && ips[mask] == 0) mask++; if(mask != 8){ //掩码后半部分不能有1 arr[5]++; continue; } if(ips[0] >= 1 && ips[0] <= 126) arr[0]++; //A类地址 else if(ips[0] >= 128 && ips[0] <= 191) arr[1]++; //B类地址 else if(ips[0] >= 192 && ips[0] <= 223) arr[2]++; //C类地址 else if(ips[0] >= 224 && ips[0] <= 239) arr[3]++; //D类地址 else if(ips[0] >= 240 && ips[0] <= 255) arr[4]++; //E类地址 if(ips[0] == 10) arr[6]++; //私有地址10开头的 else if(ips[0] == 172 && (ips[1] >= 16 && ips[1] <= 31)) arr[6]++; //私有地址172.16.0.0-172.31.255.255 else if(ips[0] == 192 && ips[1] == 168) arr[6]++; //私有地址192.168.0.0-192.168.255.255 } for(int i = 0; i < 7; i++){ //输出 cout << arr[i]; if(i != 6) cout << " "; } cout << endl; return 0; } #includeHJ21 简单密码 HJ39 判断两个IP是否属于同一子网 HJ58 输入n个整数,输出其中最小的k个#include using namespace std; int main(){ string s; while(cin >> s){ if(s.length() <= 8){ //长度不超过不可行 cout << "NG" << endl; continue; } int flag[4] = {0}; for(int i = 0; i < s.length(); i++){ if(s[i] >= 'A' && s[i] <= 'Z') //大写字母 flag[0] = 1; else if(s[i] >= 'a' && s[i] <= 'z') //小写字母 flag[1] = 1; else if(s[i] >= '0' && s[i] <= '9') //数字 flag[2] = 1; else //其他符号 flag[3] = 1; } if(flag[0] + flag[1] + flag[2] + flag[3] < 3){ //符号少于三种 cout << "NG" << endl; continue; } bool repute = false; //记录重复子串 for(int i = 0; i <= s.length() - 6; i++) //遍历检查是否有长度为3的相同的字串 for(int j = i + 3; j < s.length(); j++) if(s.substr(i, 3) == s.substr(j, 3)){ repute = true; break; } if(repute) //有重复 cout << "NG" << endl; else cout << "OK" << endl; } return 0; } //直接进行排序,然后输出最小的k个即可 #includeusing namespace std; int main(){ int n,k;//分别记录数字总个数和输出的个数 //有多组输入 while(cin>>n>>k){ //创建容器,记录每一个数字 vector num; //将数字存入容器,初始化 for(int i=0;i int temp; cin>>temp; num.push_back(temp); } //用C++自带库函数进行排序,sort()用的是快排 sort(num.begin(), num.end()); for(int i=0;i cout< HJ65 查找两个字符串a,b中的最长公共子串 HJ105 记负均正II #includeHJ31 单词倒排using namespace std; int main(){ int num; float total = 0.0; int count1 = 0,count2 = 0; while(cin>>num){ if(num>=0){ //非负数 count1++; total += num; } else{ //负数 count2++; continue; } } //输出负数的值 cout< printf("0.0"); } else{ //输出非负数的平均值 res = total/count1; printf("%.1f",res); } #includeusing namespace std; int main(){ string s;//用来存放未处理过的句子 vector words; int sublen = 0; getline(cin, s); //处理非英文字符 int len = s.size(); for(int i=0;i if((s[i]>='a'&& s[i]<='z')||(s[i]>='A'&& s[i]<='Z')){ sublen++; continue; } else{ if(sublen>0){ words.push_back(s.substr(i-sublen,sublen)); sublen = 0;//重置 } } } if(sublen>0) words.push_back(s.substr(len-sublen,sublen)); for(int i = words.size()-1;i>=0;i--) cout< 二叉树 144 二叉树的前序遍历 class Solution { public: //递归方法 void preorder(TreeNode* root, vector104. 二叉树的最大深度&res){ if (root == nullptr) { return; } res.push_back(root->val); preorder(root->left,res); preorder(root->right,res); } vector preorderTraversal(TreeNode* root) { vector res; preorder(root,res); return res; } }; class Solution { public: int maxDepth(TreeNode* root) { if(root == nullptr){ return 0; } int L = maxDepth(root -> left); int R = maxDepth(root -> right); return std::max(L,R)+1; } };列表 HJ48 从单向链表中删除指定值的节点#include#include #include using namespace std; //方法一:数组模拟链表 int main(){ int n,head; while(cin>>n>>head){ //用数组来模拟链表 vector array; //数组添加第一个元素作为表头 array.push_back(head); //添加链表元素 for(int i=1;i int num,pos_num; cin>>num>>pos_num; auto iter = find(array.begin(), array.end(), pos_num); if(iter == array.end()){ array.push_back(num); } else{ array.insert(iter+1,num); } }//for int remove; cin>>remove; array.erase(find(array.begin(), array.end(), remove)); for(int i=0; i cout< #include#include #include using namespace std; //构建链表节点 struct node{ int val; struct node* next = NULL; }; //链表法 int main(){ int n,val; while(cin>>n>>val){ node* head = new node;//头节点 head->val = val; for(int i=1;i int pre,cur; cin>>cur>>pre;//输入两个为一组的节点 node* p = new node; p->val = cur; node* q = head; //遍历找到前序节点 while(q->val != pre) q = q->next; p->next = q->next; q->next = p; }//创建链表 int remove; cin>>remove; node* p =head; while(p!=NULL){ if(p->val != remove) //不输出remove,其他都输出 cout << p->val << " "; p = p->next; } cout< HJ53 杨辉三角的变形 #includeusing namespace std; //这是一个找规律的题 int main(){ int row; while(cin>>row){ if(row == 1 || row == 2){ cout<<-1< cout<<3< cout<<2< cout<<4< HJ68 成绩排序 #includeusing namespace std; struct student{ string name; int score; }; //重载compare函数 bool compare0(student a,student b){ return a.score>b.score; } bool compare1(student a,student b){ return a.score //设置变量,确定数据条数和排序方式 int n, sort_way; cin>>n>>sort_way; //用键值对或是定义一个结构体来存放每位同学的姓名和成绩 vector studentForm; for(int i=0; i //输入成绩单 student temp; cin>>temp.name>>temp.score; studentForm.push_back(temp); } //判断排序方式,并对成绩进行排序 if(sort_way == 0){ stable_sort(studentForm.begin(), studentForm.end(),compare0); }else{ stable_sort(studentForm.begin(), studentForm.end(),compare1); } for(int i=0;i cout< HJ69 矩阵乘法 #include数字与数学思维 HJ5 进制转换using namespace std; int main(void){ int x,y,z; while(cin>>x>>y>>z){ //用数组来存储矩阵 int num; int A[x][y]; int B[y][z]; int C[x][z]; //初始化矩阵A和矩阵B for(int i=0;i for(int j=0;j cin>>num; A[i][j] = num; } } for(int i=0;i for(int j=0;j cin>>num; B[i][j] = num; } } //计算矩阵C for(int i=0;i for(int j=0;j C[i][j] = 0; //初始化矩阵 C 的每个元素,置为 0。若缺少这个,有些测试示例不通过。 for(int k=0;k C[i][j] += A[i][k] * B[k][j]; } } }//for //输出矩阵C for(int i=0;i for(int j=0;j cout << C[i][j] << ' '; } cout << endl; } } return 0; } #include#include using namespace std; //套用现成库函数 int main(){ string str; while(cin>>str){ cout< HJ6 质数因子 #include "stdio.h" #include "math.h" int main() { long int n; scanf("%ld", &n); long int k = sqrt(n); for (int i=2; i<=k; i++) { while (n%i == 0) { printf("%d ", i); n /= i; } } if(n - 1 > 0) //自己就是质数 printf("%d ", n); return 0; }#include#include using namespace std; void recursion(long n){//递归函数 for(long i = 2; i <= sqrt(n); i++){ //每次遍历到n的开方就行了 if(n % i == 0){ cout << i << " "; recursion(n / i); //递归解决后续更小的 return; } } if(n - 1 > 0) //自己就是质数 cout << n << " "; } int main(){ long n; cin >> n; recursion(n); return 0; }



