Manchester


私信TA

用户名:wenyajie

访问量:312276

签 名:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

等  级
排  名 1
经  验 62655
参赛次数 1
文章发表 188
年  龄 0
在职情况 学生
学  校 Xiamen University
专  业 计算机科学

  自我简介:

在历史前进的逻辑中前进,这个逻辑就是人心向背的逻辑

解题思路:
定义四个变量 且初值设置为0:

   int numofdaxie;  等于0表示大写没出现,等于1表示出现
   int numofxiaoxie;等于0表示小写没出现,等于1表示出现

   int shuzi;等于0表示数字没出现,等于1表示出现

   int specialword;等于0表示特殊没出现,等于1表示出现

 

     当密码长度小于8或大于16密码不合格;
    当密码长度大于等于8并且numofdaxie+numofxiaoxie+shuzi+specialword>=3时,密码合格;

    当密码长度大于等于8并且numofdaxie+numofxiaoxie+shuzi+specialword<3时,密码不合格;

  
   若长度符合条件,从第一个字符开始遍历,比如当大写字母首次出现时,把numofdaxie置为1,之后再次出现大写字母,不做  任何操作;其它三种字符处理同理;

      if(password[i]>='A'&&password[i]<='Z'&&(*numofdaxie==0))
         (*numofdaxie)++;

每次遍历时,判断numofdaxie+numofxiaoxie+shuzi+specialword是否大于等于3,是则输出YES且跳出循环;

若循环正常结束没有跳出,则说明numofdaxie+numofxiaoxie+shuzi+specialword<3,判断一下输出NO即可;


注意事项:

输出带换行符,每判断新的密码上述4个变量初值更新为0;

参考代码:

#include<stdio.h>
#include<string.h>
void function(char *password,int *numofdaxie,int *numofxiaoxie,int *shuzi,int *specialword);
void format(int *numofdaxie,int *numofxiaoxie,int *shuzi,int *specialword);
/*------------------------------------------------------------------------------------------------*/
int main()
{

   char password[20];
   int n;
   int numofdaxie;
   int numofxiaoxie;
   int shuzi;
   int specialword;

    scanf("%d",&n);
    getchar();
      for(int i=0;i<n;i++)
         {  gets(password);
            format(&numofdaxie,&numofxiaoxie,&shuzi,&specialword);
            function(password,&numofdaxie,&numofxiaoxie,&shuzi,&specialword);

         }

  return 0;
}

/*------------------------------------------------------------------------------------------------*/
void format(int *numofdaxie,int *numofxiaoxie,int *shuzi,int *specialword)
{
  (*numofdaxie)=0;
  (*numofxiaoxie)=0;
  (*shuzi)=0;
  (*specialword)=0;

}
/*------------------------------------------------------------------------------------------------*/
void function(char *password,int *numofdaxie,int *numofxiaoxie,int *shuzi,int *specialword)
{
   int length=strlen(password);

   if(length<8||length>16)
    {
     printf("NO\n");
     return ;
    }

    for(int i=0;i<length;i++)
    {

      if(password[i]>='A'&&password[i]<='Z'&&(*numofdaxie==0))
         (*numofdaxie)++;

       if(password[i]>='a'&&password[i]<='z'&&(*numofxiaoxie==0))
         (*numofxiaoxie)++;

          if(password[i]>='0'&&password[i]<='9'&&(*shuzi==0))
           (*shuzi)++;

            if((password[i]=='~'||password[i]=='!'||password[i]=='@'||password[i]=='#'||password[i]=='$'||password[i]=='%'||password[i]=='^')&&(*specialword==0))
              (*specialword)++;

               if((*numofdaxie)+(*numofxiaoxie)+(*shuzi)+(*specialword)>=3)
                {printf("YES\n");break;}

    }
            
            if((*numofdaxie)+(*numofxiaoxie)+(*shuzi)+(*specialword)<3)
                printf("NO\n");
return;
}

别忘点赞哦-.-

 

0.0分

19 人评分

  评论区

花里胡哨
2021-10-26 13:30:09
#include <iostream>
#include <cctype>
#include <cstring>
int main()
{
	int M;
	std::cin>>M;
	for(int j = 0;j < M;j++){
		char a[50];
		std::cin>>a;
		std::string str = a;//存到字符串中,方便求长度
		int n = str.length();//获取密码的长度
		if(n < 8||n > 16){
			std::cout<<"NO"<<std::endl;
			continue; //密码太短,跳进下一循环
		}
		int b,c,d,e;
		b = c = d = e = 0;
		for(int i =0;i < n;i++){
			b += ispunct(a[i]);
			c += isdigit(a[i]);
			d += islower(a[i]);
			e += isupper(a[i]);
		}
		int f = (b>0) + (c>0) + (d>0) + (e>0);//用逻辑运算计算出现的字符种类
		if(f >= 3)
			std::cout<<"YES"<<std::endl;
		else
			std::cout<<"NO"<<std::endl;
2021-10-24 16:23:26
为什么要在输入n后面加一个getchar()呢?
2020-12-30 15:39:30
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(){
	int M,p;
	scanf("%d",&M);
	getchar();
	for(p=0;p<M;p++){
		int big=0,small=0,num=0,other=0;
		char a[50];
		int l,i,n;
		gets(a);
		l=strlen(a);
		for(i=0;i<l;i++){
			if(a[i]>='A' && a[i]<='Z'){
				big=1;
			}
			else if(a[i]>='a' && a[i]<='z'){
				small=1;
			}
			else if(a[i]>='0' && a[i]<='9'){
				num=1;
			}
			else{
				other=1;
			}
		}
		n=big+small+num+other;
		if(n>=3 && l>=8 && l<=16){
			printf("YES\n");
		}
		if(n<3 || l<8 || l>16){
		printf("NO\n");
		}
	}
	return 0;
}
不知道纳出了问题  总提示错误一半
2019-03-11 18:10:39
完全看不懂
2018-11-23 21:12:20
  • «
  • 1
  • »