这题目也太随意了吧,记错考试时间,7点10开始做,都能提前交卷。
第一题:判断密码是否符合要求:
给一系列密码:每个密码用空格分隔
(1)同时具有大写、小写、符号、数字输出0;
(2)长度不在8-120之间,输出1;
(3)类型不符合输出2;
#include <bits/stdc++.h> using namespace std; int Process(string &code) { int n = code.length(); if (n < 8 || n>120) return 1; bool num = false, symbol = false, D = false, X = false; for (int i = 0; i < n; ++i) { if (num == true && symbol == true && D == true && X == true) return 0; if (code[i] >= '0' && code[i] <= '9') num = true; if (code[i] >= 'a' && code[i] <= 'z') X = true; if (code[i] >= 'A' && code[i] <= 'Z') D = true; else symbol = true; } return 2; } int main() { vector<string> str; string s = ""; while (cin >> s) { str.emplace_back(s); } vector<int> ret; for (auto &s : str) { ret.emplace_back(Process(s)); } for (auto &v : ret) cout << v << "\n"; } 第二题:剑指offer原题:矩阵中的路径 坑的是,开始没有给二维网格,后面更新题目,给了二维网格#include <bits/stdc++.h> using namespace std; int dir[5] = { -1,0,1,0,-1 }; bool isExist(vector<vector<char>> &vec, string &s, int i, int j, int pos) { if (pos == s.length()) return true; if (i < 0 || i >= vec.size() || j < 0 || j >= vec[0].size()) return false; if (vec[i][j] != s[pos] || vec[i][j] == '#') return false; char ch = vec[i][j]; vec[i][j] = '#'; for (int k = 0; k < 4; ++k) { if (isExist(vec, s, i + dir[k], j + dir[k + 1], pos + 1)) return true; } vec[i][j] = ch; return false; } int main() { vector<vector<char>> vec; vector<char> row = { 'A','B','C','E' }; vec.emplace_back(row); row[0] = 'S'; row[1] = 'F'; row[2] = 'C'; row[3] = 'S'; vec.emplace_back(row); row[0] = 'A'; row[1] = 'D'; row[2] = 'E'; row[3] = 'E'; vec.emplace_back(row); string s; cin >> s; for (int i = 0; i < s.length(); ++i) { //统一大小写 if (s[i] >= 'a') s[i] = s[i] - 32; } cout << s << endl; for (int i = 0; i < vec.size(); ++i) { for (int j = 0; j < vec[0].size(); ++j) { if (vec[i][j] == s[0]) { if (isExist(vec, s, i, j, 0)) { cout << "true" << endl; return 0; } } } } cout << "false" << endl; return 0; }
全部评论
(8) 回帖