「PAT乙级真题解析」Basic Level 1081 检查密码 (问题分析+完整步骤+伪代码描述+提交通过代码)
创始人
2024-04-09 05:46:39
0

乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。

PAT (Basic Level) Practice 1081 检查密码

问题分析

题设要求按照给定标准检查密码是否合法。标准已明确给出, 核心在于把标准准确表示为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)

完整提交代码

/*
# 问题分析
题设要求按照给定标准检查密码是否合法。标准已明确给出, 核心在于把标准准确表示为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;
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...