题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

#include <iostream>
#include <string>
using namespace std;

bool IsValid(const std::string& passport) {
  if (passport.size() <= 8) {
    return false;
  }
  int has_up_char = 0;
  int has_low_char = 0;
  int has_digit = 0;
  int has_others_char = 0;
  for (const char c : passport) {
    if (c >= 'A' && c <= 'Z') {
      has_up_char = 1;
    } else if (c >= 'a' && c <= 'z') {
      has_low_char = 1;
    } else if (c >= '0' && c <= '9') {
      has_digit = 1;
    } else {
      if (c != ' ' && c != '\n') {
        has_others_char = 1;
      }
    }
  }
  int sum = has_digit + has_low_char + has_others_char + has_up_char;
  if (sum < 3) {
    return false;
  }

  for (int pos = 0; pos < passport.size() - 2; pos++) {
    for (int len = 3; pos + len < passport.size(); len++) {
      std::string str = passport.substr(pos, len);
      if (passport.find(str, pos + len) != std::string::npos) {
        return false;
      }
    }
  }

  return true;
}

int main() {
  std::string passport;
  while (getline(cin, passport)) {
    if (IsValid(passport)) {
      std::cout << "OK" << std::endl;
    } else {
      std::cout << "NG" << std::endl;
    }
  }

  return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

绝迹的星:前端和后端写两份简历, 如果想干全栈就直接写求职意向为全栈工程师
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务