乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。
PAT (Basic Level) Practice 1081 检查密码
题设要求按照给定标准检查密码是否合法。标准已明确给出, 核心在于把标准准确表示为if条件。
密码长度不少于6个字符
密码不能够存在英文字母、数字和小数点之外的其他字符
需要同时包含字母和数字
/*
# 问题分析
题设要求按照给定标准检查密码是否合法。标准已明确给出, 核心在于把标准准确表示为if条件。## 标准一
密码长度不少于6个字符
- if len(password) < 6: return False## 标准二
密码不能够存在英文字母、数字和小数点之外的其他字符
- for char in password:- if char != '.' and not ('0'<= char '9') and not ('a'<= char 'z') and not ('A'<= char 'Z'):- return False
- return True## 标准三
需要同时包含字母和数字
- contained_digit = False
- contained_alpabet = False
- for char in password:- if '0' <= char <= '9':- contained_digit = True- else if 'a' <= char <= 'z':- contained_alpabet = True- else if 'A' <= char <= 'Z':- contained_alpabet = True
- if not contained_digit or not contained_alpabet:- return False
- return True# 完整步骤描述
1. 获取输入: 用户输入密码
2. 如果密码长度小于6:- 输出"Your password is tai duan le."- 结束程序
3. 初始化记录器:- 密码包含数字标志位 = False- 密码包含英文字母标志位 = False
4. 对于用户输入密码的每一个字符:- 如果字符不是'.', 且不是数字, 且不是英文字母:- 输出"Your password is tai luan le."- 结束程序- 如果字符是英文字母:- 密码包含英文字母标志位 = True- 如果字符是数字:- 密码包含数字标志位 = True
5. 如果密码包含数字标志位为False:- 输出"Your password needs shu zi."- 结束程序
6. 如果密码包含英文字母标志位为False:- 输出"Your password needs zi mu."- 结束程序
7. 输出"Your password is wan mei."# 伪代码描述
1. get input: password
2. if length(password) < 6:- print("Your password is tai duan le.");- end procedure;
3. init recorder:- contained_digit = False;- contained_alpabet = False;
4. for char in password:- if char == '.':- continue;- if '0' <= char <= '9':- contained_digit = True;- if 'a' <= char <= 'z' or 'A' <= char <= 'Z':- contained_alpabet = True;- else:- print("Your password is tai luan le.");- end procedure;
5. if not contained_digit:- print("Your password is tai luan le.");- end procedure;
6. if not contained_alpabet:- print("Your password needs zi mu.");- end procedure;
7. print("Your password is wan mei.");# 注意事项
1. 输入中可能有空格(测试点2)
*/# include
# define PERFECT 0
# define TOO_SHORT 1
# define INVALID_CHAR 2
# define NO_DIGIT 3
# define NO_ALPHABET 4int check_password(char* password){int contained_digit = 0;int contained_alpabet = 0;int length = strlen(password);if (length < 6) return TOO_SHORT;for (int i = 0; password[i]; i++){if (password[i] == '.'){continue;}else if (password[i] >= '0' && password[i] <= '9'){contained_digit = 1;}else if (password[i] >= 'a' && password[i] <= 'z'){contained_alpabet = 1;}else if (password[i] >= 'A' && password[i] <= 'Z'){contained_alpabet = 1;}else{return INVALID_CHAR;}}if (contained_digit == 0) return NO_DIGIT;if (contained_alpabet == 0) return NO_ALPHABET;return PERFECT;
}int main(){int case_amount;scanf("%d", &case_amount);getchar();char password[90];for (int i = 0; i < case_amount; i++){fgets(password, 90, stdin);password[strlen(password) - 1] = 0;int check_result = check_password(password);if (check_result == PERFECT) printf("Your password is wan mei.\n");if (check_result == TOO_SHORT) printf("Your password is tai duan le.\n");if (check_result == INVALID_CHAR) printf("Your password is tai luan le.\n");if (check_result == NO_DIGIT) printf("Your password needs shu zi.\n");if (check_result == NO_ALPHABET) printf("Your password needs zi mu.\n");}return 0;
}
上一篇:c语言(看一遍就会操作,小马教一步步教你如何文件操作)
下一篇:K8s集群环境搭建