规则 2~5 问“是否至少有一个”(any),规则 6 问“是否全部都是”(all)。 把规则 6 写成 any,只要有一个允许的字符就会通过。 要输出所有违反的规则,所以要分开写六个 if,而不是 else if 链;写成链只会输出第一个违反的规则。 长度要像 8 <= len <= 16 这样包含两端。 判断范围的写法因语言而异。 只有 Python 能连写 "A" <= c <= "Z"。 在 C、C++、JS 中 'A' <= c <= 'Z' 会被读成 ('A' <= c) <= 'Z'。 在 C、C++ 中永远为真(较新的 clang 会直接报错),在 JS 中会把布尔值和 'Z' 当数字比较,永远为假。 TS、Java、C# 无法编译。 所以要写 c >= 'A' && c <= 'Z'。 与、或、非在 Python 中是 and、or、not,其他语言是 &&、||、!,而且在所有语言中 and 都比 or 先结合(a and b or c 是 (a and b) or c)。 “这个字符是否在特殊字符列表里”在 Python 中是 c in s,JS/TS s.includes(c),Java s.indexOf(c) >= 0,C# s.Contains(c),C++ s.find(c) != npos,C strchr(s, c)。 Craykon 用 SPECIAL.包含(c),与、或、非是 &&、||、!。
Pythonmain.py · 25 行
SPECIAL = "!@#$%^&*"def broken_rules(pw): broken = [] if not 8 <= len(pw) <= 16: broken.append(1) if not any("A" <= c <= "Z" for c in pw): broken.append(2) if not any("a" <= c <= "z" for c in pw): broken.append(3) if not any("0" <= c <= "9" for c in pw): broken.append(4) if not any(c in SPECIAL for c in pw): broken.append(5) # 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나 if not all(c.isascii() and c.isalnum() or c in SPECIAL for c in pw): broken.append(6) return brokenn = int(input())for _ in range(n): broken = broken_rules(input().strip()) print(" ".join(map(str, broken)) if broken else "通过")
JavaScriptmain.js · 27 行
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const SPECIAL = "!@#$%^&*";const isUpper = (c) => c >= "A" && c <= "Z";const isLower = (c) => c >= "a" && c <= "z";const isDigit = (c) => c >= "0" && c <= "9";const isSpecial = (c) => SPECIAL.includes(c);function brokenRules(pw) { const chars = [...pw]; const broken = []; if (!(pw.length >= 8 && pw.length <= 16)) broken.push(1); if (!chars.some(isUpper)) broken.push(2); if (!chars.some(isLower)) broken.push(3); if (!chars.some(isDigit)) broken.push(4); if (!chars.some(isSpecial)) broken.push(5); // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나 if (!chars.every((c) => isUpper(c) || isLower(c) || isDigit(c) || isSpecial(c))) broken.push(6); return broken;}const out = [];for (let i = 1; i <= n; i++) { const broken = brokenRules(lines[i].trim()); out.push(broken.length ? broken.join(" ") : "通过");}console.log(out.join("\n"));
TypeScriptmain.ts · 28 行
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const SPECIAL = "!@#$%^&*";const isUpper = (c: string): boolean => c >= "A" && c <= "Z";const isLower = (c: string): boolean => c >= "a" && c <= "z";const isDigit = (c: string): boolean => c >= "0" && c <= "9";const isSpecial = (c: string): boolean => SPECIAL.includes(c);function brokenRules(pw: string): number[] { const chars: string[] = [...pw]; const broken: number[] = []; if (!(pw.length >= 8 && pw.length <= 16)) broken.push(1); if (!chars.some(isUpper)) broken.push(2); if (!chars.some(isLower)) broken.push(3); if (!chars.some(isDigit)) broken.push(4); if (!chars.some(isSpecial)) broken.push(5); // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나 if (!chars.every((c) => isUpper(c) || isLower(c) || isDigit(c) || isSpecial(c))) broken.push(6); return broken;}const out: string[] = [];for (let i = 1; i <= n; i++) { const broken: number[] = brokenRules(lines[i].trim()); out.push(broken.length ? broken.join(" ") : "通过");}console.log(out.join("\n"));
JavaMain.java · 44 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;public class Main { static final String SPECIAL = "!@#$%^&*"; static List<Integer> brokenRules(String pw) { boolean upper = false, lower = false, digit = false, special = false, allAllowed = true; for (char c : pw.toCharArray()) { boolean u = c >= 'A' && c <= 'Z', l = c >= 'a' && c <= 'z', d = c >= '0' && c <= '9'; boolean s = SPECIAL.indexOf(c) >= 0; upper |= u; lower |= l; digit |= d; special |= s; // 6: 모든 글자가 넷 중 하나 — 하나라도 아니면 거짓 if (!(u || l || d || s)) allAllowed = false; } List<Integer> broken = new ArrayList<>(); if (!(pw.length() >= 8 && pw.length() <= 16)) broken.add(1); if (!upper) broken.add(2); if (!lower) broken.add(3); if (!digit) broken.add(4); if (!special) broken.add(5); if (!allAllowed) broken.add(6); return broken; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { List<Integer> broken = brokenRules(in.readLine().trim()); if (broken.isEmpty()) out.append("通过"); else for (int j = 0; j < broken.size(); j++) out.append(j > 0 ? " " : "").append(broken.get(j)); out.append('\n'); } System.out.print(out); }}
C#Main.cs · 36 行
using System;using System.Collections.Generic;using System.Linq;class Program{ const string Special = "!@#$%^&*"; static bool IsUpper(char c) => c >= 'A' && c <= 'Z'; static bool IsLower(char c) => c >= 'a' && c <= 'z'; static bool IsDigit(char c) => c >= '0' && c <= '9'; static bool IsSpecial(char c) => Special.Contains(c); static List<int> BrokenRules(string pw) { var broken = new List<int>(); if (!(pw.Length >= 8 && pw.Length <= 16)) broken.Add(1); if (!pw.Any(IsUpper)) broken.Add(2); if (!pw.Any(IsLower)) broken.Add(3); if (!pw.Any(IsDigit)) broken.Add(4); if (!pw.Any(IsSpecial)) broken.Add(5); // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나 if (!pw.All(c => IsUpper(c) || IsLower(c) || IsDigit(c) || IsSpecial(c))) broken.Add(6); return broken; } static void Main() { int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { var broken = BrokenRules(Console.ReadLine().Trim()); Console.WriteLine(broken.Count == 0 ? "通过" : string.Join(" ", broken)); } }}
C++main.cpp · 37 行
#include <algorithm>#include <iostream>#include <string>#include <vector>const std::string SPECIAL = "!@#$%^&*";bool is_upper(char c) { return c >= 'A' && c <= 'Z'; }bool is_lower(char c) { return c >= 'a' && c <= 'z'; }bool is_digit(char c) { return c >= '0' && c <= '9'; }bool is_special(char c) { return SPECIAL.find(c) != std::string::npos; }std::vector<int> broken_rules(const std::string& pw) { std::vector<int> broken; if (!(pw.size() >= 8 && pw.size() <= 16)) broken.push_back(1); if (std::none_of(pw.begin(), pw.end(), is_upper)) broken.push_back(2); if (std::none_of(pw.begin(), pw.end(), is_lower)) broken.push_back(3); if (std::none_of(pw.begin(), pw.end(), is_digit)) broken.push_back(4); if (std::none_of(pw.begin(), pw.end(), is_special)) broken.push_back(5); // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나 if (!std::all_of(pw.begin(), pw.end(), [](char c) { return is_upper(c) || is_lower(c) || is_digit(c) || is_special(c); })) broken.push_back(6); return broken;}int main() { int n; std::cin >> n; for (int i = 0; i < n; i++) { std::string pw; std::cin >> pw; std::vector<int> broken = broken_rules(pw); if (broken.empty()) std::cout << "通过"; for (std::size_t j = 0; j < broken.size(); j++) std::cout << (j ? " " : "") << broken[j]; std::cout << '\n'; }}
Cmain.c · 40 行
#include <stdio.h>#include <string.h>static const char *SPECIAL = "!@#$%^&*";// c가 SPECIAL에 있나. strchr는 c == '\0'일 때도 끝의 '\0'을 찾아 주므로 c를 먼저 본다static int is_special(char c) { return c != '\0' && strchr(SPECIAL, c) != NULL; }int main(void) { int n; char pw[32]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%31s", pw); int len = strlen(pw); int upper = 0, lower = 0, digit = 0, special = 0, all_allowed = 1; for (int j = 0; j < len; j++) { char c = pw[j]; int u = c >= 'A' && c <= 'Z', l = c >= 'a' && c <= 'z', d = c >= '0' && c <= '9', s = is_special(c); upper |= u; lower |= l; digit |= d; special |= s; if (!(u || l || d || s)) all_allowed = 0; } int broken[6], k = 0; if (!(len >= 8 && len <= 16)) broken[k++] = 1; if (!upper) broken[k++] = 2; if (!lower) broken[k++] = 3; if (!digit) broken[k++] = 4; if (!special) broken[k++] = 5; if (!all_allowed) broken[k++] = 6; if (k == 0) puts("通过"); else { for (int j = 0; j < k; j++) printf(j ? " %d" : "%d", broken[j]); putchar('\n'); } } return 0;}
Craykonmain.cray · 66 行
SPECIAL = "!@#$%^&*";어긴규칙(pw){ upper = 假; lower = 假; digit = 假; special = 假; allAllowed = 真; 遍历 (c 在 pw) { u = c >= "A" && c <= "Z"; l = c >= "a" && c <= "z"; d = c >= "0" && c <= "9"; s = SPECIAL.包含(c); upper = upper || u; lower = lower || l; digit = digit || d; special = special || s; // 6: 모든 글자가 넷 중 하나 — 하나라도 아니면 거짓 如果 (!(u || l || d || s)) { allAllowed = 假; } } broken = []; 如果 (!(pw.长度 >= 8 && pw.长度 <= 16)) { broken.添加(1); } 如果 (!upper) { broken.添加(2); } 如果 (!lower) { broken.添加(3); } 如果 (!digit) { broken.添加(4); } 如果 (!special) { broken.添加(5); } 如果 (!allAllowed) { broken.添加(6); } 返回 broken;}n = 控制台.读行().转数字();循环 (i = 0; i < n; i++){ broken = 어긴규칙(控制台.读行().去空白()); 如果 (broken.数量 == 0) { 控制台.输出("通过"); } 否则 { 控制台.输出(broken.连接(" ")); }}