規則 2~5 は「一つでもあるか」(any)、規則 6 は「すべてそうか」(all)です。 6 を any で書くと、許される文字が一つあるだけで通ってしまいます。 破った規則をすべて出すので、else if の連鎖ではなく 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.つなぐ(" ")); }}