首页
学习
活动
专区
圈层
工具
发布
29 篇文章
1
PAT (Basic Level) Practice (中文)1047 编程团体赛
2
PAT (Basic Level) Practice (中文)1083 是否存在相等的差
3
PAT (Basic Level) Practice (中文)1082 射击比赛
4
PAT (Basic Level) Practice (中文)1081 检查密码
5
PAT (Basic Level) Practice (中文)1077 互评成绩计算
6
PAT (Basic Level) Practice (中文)1076 Wifi密码
7
PAT (Basic Level) Practice (中文)1064 朋友数
8
PAT (Basic Level) Practice (中文)1063 计算谱半径
9
PAT (Basic Level) Practice (中文)1057 数零壹
10
PAT (Basic Level) Practice (中文)1056 组合数的和
11
PAT (Basic Level) Practice (中文)1042 字符统计
12
PAT (Basic Level) Practice (中文)1041 考试座位号
13
PAT (Basic Level) Practice (中文)1023 组个最小数
14
PAT (Basic Level) Practice (中文)1022 D进制的A+B
15
PAT (Basic Level) Practice (中文)1019 数字黑洞
16
PAT (Basic Level) Practice (中文)1007 素数对猜想
17
PAT (Basic Level) Practice (中文)1091 N-自守数
18
PAT (Basic Level) Practice (中文)1026 程序运行时间
19
PAT (Basic Level) Practice (中文)1061 判断题
20
PAT (Basic Level) Practice (中文)1086 就不告诉你
21
PAT (Basic Level) Practice (中文)1016 部分A+B
22
PAT (Basic Level) Practice (中文)1012 数字分类
23
PAT (Basic Level) Practice (中文)1013 数素数
24
PAT (Basic Level) Practice (中文)1011 A+B 和 C
25
PAT (Basic Level) Practice (中文)1009 说反话
26
PAT (Basic Level) Practice (中文)1008 数组元素循环右移问题
27
PAT (Basic Level) Practice (中文)1006 换个格式输出整数
28
PAT (Basic Level) Practice (中文)1004 成绩排名
29
PAT (Basic Level) Practice (中文)1002 写出这个数

PAT (Basic Level) Practice (中文)1081 检查密码

1081 检查密码

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

输入格式:

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

输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下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.。

输入样例:

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

输出样例:

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.

分析:

1、计算输入字符串的长度,判断是否满足条件(>=6) 2、对于长度满足条件的字符串

  1. 判断是否只有 数字、字母和 小数点组成
代码语言:javascript
复制
char str[101][88];
int fun1(int i,int len)
{
    int j;
    int l=1;
    for(j=0;j<len;j++)
    {
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
           ||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
        else
        {
            l=0;
            break;
        }
    }
    return l;
}
  1. 判断是否只有数字或者只有字母
代码语言:javascript
复制
int fun2(int i,int len)
{
    int j;
    int temp1=0;
    int temp2=0;
    for(j=0;j<len;j++)
    {
        if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
    }
    if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
    if(temp1==0&&temp2>0) return 0;  //只有字母,没有数字
    if(temp1>0&&temp2>0) return 1;  //既有数字,又有字母
}

3、输出

代码:

代码语言:javascript
复制
#include<stdio.h>
char str[101][88];
int fun1(int i,int len)
{
    int j;
    int l=1;
    for(j=0;j<len;j++)
    {
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
           ||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
        else
        {
            l=0;
            break;
        }
    }
    return l;
}

int fun2(int i,int len)
{
    int j;
    int temp1=0;
    int temp2=0;
    for(j=0;j<len;j++)
    {
        if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
        if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
    }
    if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
    if(temp1==0&&temp2>0) return 0;  //只有字母,没有数字
    if(temp1>0&&temp2>0) return 1;  //既有数字,又有字母
}
int main()
{
    int i;
    int N;
    scanf("%d",&N);
    getchar();  //吸收回车
    for(i=0;i<N;i++)
        gets(str[i]);
    for(i=0;i<N;i++)
    {
        int len=strlen(str[i]);
       // printf("len==%d ",len);
        if(len>=6)
        {
          int temp1=fun1(i,len);
          if(temp1==1)
          {
             int temp2=fun2(i,len);
             if(temp2==-1) printf("Your password needs zi mu.\n");
             if(temp2==0) printf("Your password needs shu zi.\n");
             if(temp2==1) printf("Your password is wan mei.\n");
          }
          else printf("Your password is tai luan le.\n");
        }
        else
        {
            printf("Your password is tai duan le.\n");
        }
    }
    return 0;
}
下一篇
举报
领券