题解 | #密码强度等级#
密码强度等级
https://www.nowcoder.com/practice/52d382c2a7164767bca2064c1c9d5361
#include <stdio.h> #include <string.h> #include <ctype.h> int m_length(int n) //长度判断得分 { if (n <= 4) return 5; else if (n > 4 && n <= 7) return 10; else return 25; } int m_alpha(char string[], int len) //判断字母得分 { int m = 0, n = 0; for (int i = 0; i < len; i++) { if (isupper(string[i])) //判断是否为大写字母,否则返回非零值 { m = 1; } if (islower(string[i])) //判断是否为小写字母,否则返回非零值 { n = 1; } } if (m == 0 && n == 0) { return 0; } else if ((m == 1 && n == 0) || (m == 0 && n == 1)) { return 10; } else if (m == 1 && n == 1) { return 20; } else return 0; } int m_number(char string[], int n) //判断数字得分 { int count = 0; for (int i = 0; i < n; i++) { if (isdigit(string[i]) != 0)//判断是否为数字,否则返回非零值 count++; } if (count == 0) return 0; else if (count == 1) return 10; else return 20; } int m_punct(char string[], int n) //判断其他符号得分 { int count = 0; for (int i = 0; i < n; i++) { if (ispunct(string[i]) != 0)//判断是否为标点符号和特殊符号,否则返回非零值 count++; } if (count == 0) return 0; else if (count == 1) return 10; else return 25; } int m_reward(int a,int b,int c)//计算奖励分 { if(a!=0&&b!=0&&c==0) return 2; else if(a==10&&b!=0&&c!=0) return 3; else if(a==20&&b!=0&&c!=0) return 5; else return 0; } int main() { char string[300]; scanf("%[^\n]", string); int len = strlen(string); int m1 = m_length(len); //长度 int m2 = m_alpha(string, len); //字母 int m3 = m_number(string, len); //数字 int m4 = m_punct(string, len); //符号 int m5 = m_reward(m2,m3,m4);//奖励分 int m = m1 + m2 + m3 + m4 + m5; if(m>=90) printf("VERY_SECURE"); if(m>=80&&m<90) printf("SECURE"); if(m>=70&&m<80) printf("VERY_STRONG"); if(m>=60&&m<70) printf("STRONG"); if(m>=50&&m<60) printf("AVERAGE"); if(m>=25&&m<50) printf("WEAK"); if(m>=0&&m<25) printf("VERY_WEAK"); return 0; }
c编程学习记录 文章被收录于专栏
为了华为od机试刷题学习记录