首页 > 其他分享 >PAT Basic 1081. 检查密码

PAT Basic 1081. 检查密码

时间:2023-04-11 11:36:09浏览次数:43  
标签:输出 le PAT 1081 密码 Basic password Your 输入

PAT Basic 1081. 检查密码

1. 题目描述:

本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。

2. 输入格式:

输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。

注意: 题目保证不存在只有小数点的输入。

3. 输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下5种:

  • 如果密码合法,输出Your password is wan mei.
  • 如果密码太短,不论合法与否,都输出Your password is tai duan le.
  • 如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.
  • 如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.
  • 如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.

4. 输入样例:

5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6

5. 输出样例:

Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.

6. 性能要求:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

考察基础IO,有几处细节还是需要注意下:

  • 这里需要用getchar()消耗掉正整数N后面的'\n'以使得后续输入正常。
  • 因为密码中可能含有空格符,所以不能使用scanf()输入,我选择使用fgets()输入,要注意fgets()会存储末尾的'\n'
  • 这里新学了<ctype.h>中的库函数isalpha()isdigit()用于字母和数字判断。

编写子函数judgePassword()进行逻辑判断和输出,第一次提交时testpoint1报wrong answer,检查后发现因为fgest()会存储末尾换行符,所以我这里判断密码长度时阈值应该+1,修改后AC。

My Code:

#include <stdio.h>
#include <string.h> // strlen header
#include <ctype.h> // isalpha header, isdigit header

#define MAX_LEN (80+2) // maxLen + '\n' + '\0'

void judgePassword(const char *password);

// first submit testpoint1 wrong answer
int main(void)
{
    int keyCount = 0;
    char password[MAX_LEN];
    int i=0; // iterator
    
    scanf("%d", &keyCount);
    getchar(); // consume the '\n' after keyCount
    
    for(i=0; i<keyCount; ++i)
    {
        // fgets store the '\n'
        fgets(password, MAX_LEN, stdin);
        //printf("%s", password); // test input
        judgePassword(password);
    }
    
    return 0;
}

void judgePassword(const char *password)
{
    int illegalFlag = 0;
    int letterFlag = 0;
    int digitFlag =0;
    int i=0; // iterator
    
    // here fixed testpoint1, for '\n' will let strlen +1
    if(strlen(password) < 6+1) // length is too short
    {
        printf("Your password is tai duan le.\n");
    }
    else // length meet require
    {
        i=0;
        while(password[i] != '\n') // traverse the password
        {
            //int isalpha(int c);
            if(isalpha(password[i]))
            {
                letterFlag = 1;
            }
            //int isdigit(int c);
            else if(isdigit(password[i]))
            {
                digitFlag = 1;
            }
            else if(password[i] != '.')
            {
                illegalFlag = 1;
            }
            ++i;
        }
        
        if(illegalFlag)
        {
            printf("Your password is tai luan le.\n");
        }
        else
        {
            if(letterFlag && !digitFlag)
            {
                printf("Your password needs shu zi.\n");
            }
            else if(digitFlag && !letterFlag)
            {
                printf("Your password needs zi mu.\n");
            }
            else if(digitFlag && letterFlag) // add this if doesn't pass testpoint1
            {
                printf("Your password is wan mei.\n");
            }
        }
    }
    
}

标签:输出,le,PAT,1081,密码,Basic,password,Your,输入
From: https://www.cnblogs.com/tacticKing/p/17305652.html

相关文章

  • 高性能 Jsonpath 框架,Snack3 3.2.65 发布
    高性能Jsonpath框架,Snack33.2.65发布来源:投稿作者: 梅子酒好吃2023-04-1014:18:00 0Snack3,一个高性能的JsonPath框架借鉴了Javascript所有变量由var申明,及Xmldom一切都是Node的设计。其下一切数据都以ONode表示,ONode也即Onenode之意,代......
  • @RequestParam和@PathVariable的用法与区别
    **@PathVariable**格式@RequestMapping(value="/user/{username}")publicStringuserProfile(@PathVariable(value="username")Stringusername){ return"user"+username;}在上面的例子中,当@Controller处理HTTP请求时,userProfile的参数......
  • PAT Basic 1080. MOOC期终成绩
    PATBasic1080.MOOC期终成绩1.题目描述:对于在中国大学MOOC(http://www.icourse163.org/)学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分(满分100)。总评成绩的计算公式为\(G=(G_{mid−term}×40\%+G_{final}×......
  • 利用 curl 发送 post/get/del/put/patch 请求 PHP
    因为需要在php开发中对接其它接口需要用phpcurl去对接其它接口我把他们封装成函数。这里面是封装好的会自动把data进行转成json格式,同时解码成php数组输出get请求:<?phpfunctiongeturl($url){$headerArray=array("Content-type:application/json;","Acc......
  • 【spring学习笔记】(二)Spring MVC注解配置 参数转换注解@RequestMapping@RequestParam
    @TOC介绍在SpringMVC项目中,<\context:component-scan>配置标签还会开启@Request-Mapping、@GetMapping等映射注解功能(也就是会注册RequestMappingHandler-Mapping和RequestMappingHandlerAdapter等请求映射和处理等组件),但是<context:component-scan>不支持数据转换或验证等注解功......
  • golang 编译碰到问题 Package python-2.7 was not found in the pkg-config search pa
    golang运行单测或者编译程序时提示需要配置PKG_CONFIG_PATH环境变量,原因是在程序里使用了go-python包,要求运行环境有python2.7,并设置PKG_CONFIG_PATH环境变量,解决方案如下:#pkg-config--cflags--python-2.7Packagepython-2.7wasnotfoundinthepkg-configsear......
  • Vue——patch.ts【十四】
    前言前面我们简单的了解了vue初始化时的一些大概的流程,这里我们扩展下Vue的patch。内容这一块主要围绕vue中的__patch__进行剖析。__patch__Vue.prototype.__patch__的方法位于scr/platforms/web/runtime/index.ts中;//installplatformpatchfunction//判断是......
  • Python DeprecationWarning: executable_path has been deprecated, please pass in a
    借鉴https://blog.csdn.net/lly1122334/article/details/106217320https://blog.csdn.net/qq_57377057/article/details/128463296https://blog.csdn.net/tangya3158613488/article/details/106902110 将之前谷歌浏览器的105版本替换为110版本解决Python:DeprecationWar......
  • Xpath定位-高级定位
    Xpath语法:https://www.w3school.com.cn/xpath/xpath_syntax.asp包含-contains()Xpath 表达式中的一个函数contains()函数匹配==属性值==中包含的==字符串== //*[contains(@属性,"属性值")]contains() 函数定位的元素很容易为 listcontains() 函数内的属性名需要用 @......
  • PAT Basic 1079. 延迟的回文数
    PATBasic1079.延迟的回文数1.题目描述:给定一个\(k+1\)位的正整数\(N\),写成\(a_k⋯a_1a_0\)的形式,其中对所有\(i\)有\(0≤a_i<10\)且\(a_k>0\)。\(N\)被称为一个回文数,当且仅当对所有\(i\)有\(a_i=a_{k−i}\)。零也被定义为一个回文数。非回文数也可以通过一......