- 题目分为3部分:基础选择10题30分、专项选择10题30分、编程2题40分
- 编程题1:A 100%, 是字符串匹配的
- 编程题2:A 67%,可能是复杂度过高导致不能100%。类似于迷宫寻路
- 编程题1代码如下
#include <iostream> #include <vector> #include <string> using namespace std; int main() { // 输入, 然后开始分割 string str; getline(std::cin, str); vector<string> vec; // 分割字符串, 然后逐个放到vec中 int count; int currentIndex; for(int index = 0; index < str.size(); ++index) { count = 0; currentIndex = index; while(str[index] != ' ' && index < str.size()) { count++; index++; } string temp(str, currentIndex, count); vec.push_back(temp); } bool isNumber; bool isFuHaO; bool large; bool small; // 遍历每个字符串 for(const auto & elem : vec) { isNumber = false; isFuHaO = false; large = false; small = false; // 长度不符合 if(elem.size() < 8 || elem.size() > 120) std::cout << 1 << std::endl; else { // 遍历每个字符类型不符合返回2 for(int index = 0; index < elem.size(); ++index) { if(elem[index] >= 65 && elem[index] <= 90) large = true; else if(elem[index] >= 48 && elem[index] <= 57) isNumber = true; else if(elem[index] >= 97 && elem[index] <= 122) small = true; else isFuHaO = true; } if(large && isNumber && small && isFuHaO) std::cout << 0 << std::endl; else std::cout << 2 << std::endl; } } return 0; } /*A 65 Z 90 0 48 9 57 a 97 z 122*/
- 编程题2代码如下
#include <iostream> using namespace std; bool func(char (*str)[4], int rows, int cols, int row, int col, char s[], int index); int main() { char arr[][4] = {{'A','B','C','E'}, {'S','F','C','S'}, {'A','D','E','E'}}; int rows = 3; int cols = 4; char str[20]; cin >> str; // 开始查找 for(int i = 0; i < rows; ++i) { for(int j = 0; j < cols; j++) { // 从每一个地方开始开始遍历, 只要以str[0]开头的就开始遍历 if(arr[i][j] == str[0]) { if(func(arr, rows, cols, i, j, str, 0)) { std::cout << "true" << std::endl; return 0; } } } } std::cout << "false" << std::endl; return 0; } bool func(char (*str)[4], int rows, int cols, int row, int col, char s[], int index) { // 遍历完了, 返回true if(*(s + index) == '\0') return true; // 超出界限了, 返回false if(row < 0 || row >= rows || col < 0 || col >= cols) return false; /*// 两个字符不相等, 返回false if(str[row][col] != *(s + index)) return false;*/ // 递归向上下左右开始遍历 return func(str, rows, cols, row + 1, col, s, index + 1) || func(str, rows, cols, row - 1, col, s, index + 1) || func(str, rows, cols, row, col + 1, s, index + 1) || func(str, rows, cols, row, col - 1, s, index + 1); }
全部评论
(2) 回帖