정리를 좋아하는 학교는 "정리된 번호"가 붙은 사물함을 먼저 나누어 준다.
어떤 수를 왼쪽 자리부터 읽을 때 모든 자리의 숫자가 바로 왼쪽 자리의 숫자보다 크거나 같으면 정리된 번호다.
예를 들어 1123, 7, 0은 정리된 번호이고, 1231(3 다음에 1)과 10은 아니다.
한 자리 수는 모두 정리된 번호다.
첫 줄에 사물함 번호의 수 n, 다음 n줄에 번호가 하나씩 주어진다.
번호마다 정리된 번호이면 "예", 아니면 "아니요"를 한 줄에 출력하라.
마지막 줄에는 정리된 번호의 개수를 출력하라.
번호 하나를 받아 정리된 번호인지 참·거짓으로 돌려주는 함수를 만들어 쓰자.
입력
1 ≤ n ≤ 1,000. 0 ≤ 번호 < 10^18(최대 18자리)이고, 0이 아니면 맨 앞자리는 0이 아니다.
출력
n+1줄: 번호마다 yes 또는 no, 마지막 줄에 yes의 개수
예시 1 · stdin
4
1123
1231
7
10
stdout
예
아니요
예
아니요
2
main.py
오류 목록00F8 ↓ · ⇧F8 ↑
입력(stdin)
실행하면 이 줄들이 차례로 input()·표준 입력으로 들어가요(실행 중에 묻지 않아요).
$ 준비됐어요
코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.
다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이
참·거짓을 돌려주는 함수는 "틀린 곳을 하나라도 찾으면 곧바로 거짓, 끝까지 다 보고 나서야 참"으로 짜요. 참거짓 타입 이름이 언어마다 달라요: Python bool(True/False), JS·TS boolean, Java boolean, C# bool, C++ bool, C는 <stdbool.h>의 bool(예전 C는 int로 0·1을 돌려줬어요), 크레이콘은 값 참/거짓을 돌려주기로 돌려줘요. Python은 all(a <= b for a, b in zip(s, s[1:])) 한 줄로도 써요. 두 숫자 글자는 글자 코드 순서가 곧 숫자 순서라서 수로 바꾸지 않고 s[i] > s[i + 1]로 바로 비교해도 돼요. 함정 넷: ① 반복 안에서 if 틀림: return False 다음에 else: return True를 붙이면 첫 짝만 보고 참을 돌려줘요(1231이 yes). ② 같은 숫자가 이어지는 것(112)도 정리된 번호예요. 작아질 때만 거짓이니 >=가 아니라 >로 물어요. ③ JS forEach 안의 return false는 콜백 하나만 끝내고 바깥 함수는 끝내지 않아서, 함수가 늘 true를 돌려줘요. 중간에 나가려면 for 반복이나 every를 써요(Java·C#의 람다 안에서 return false를 쓰면 아예 컴파일되지 않아요). ④ 번호가 18자리까지라 int(약 21억까지)에 들어가지 않아요. Java Integer.parseInt는 예외를 내고 C scanf("%d")는 넘쳐요. 문자열로 읽으면 크기 걱정이 없어요.
Pythonmain.py · 17 줄
def is_tidy(s): """왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 True""" for i in range(len(s) - 1): if s[i] > s[i + 1]: return False return True # 끝까지 다 본 뒤에야 참이라고 말할 수 있다n = int(input())count = 0for _ in range(n): if is_tidy(input().strip()): print("예") count += 1 else: print("아니요")print(count)
JavaScriptmain.js · 23 줄
const lines = require("fs").readFileSync(0, "utf8").split("\n");// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 truefunction isTidy(s) { for (let i = 0; i + 1 < s.length; i++) { if (s[i] > s[i + 1]) return false; } return true;}const n = Number(lines[0]);const out = [];let count = 0;for (let i = 1; i <= n; i++) { if (isTidy(lines[i].trim())) { out.push("예"); count++; } else { out.push("아니요"); }}out.push(count);console.log(out.join("\n"));
TypeScriptmain.ts · 25 줄
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 truefunction isTidy(s: string): boolean { for (let i = 0; i + 1 < s.length; i++) { if (s[i] > s[i + 1]) return false; } return true;}const n: number = Number(lines[0]);const out: string[] = [];let count = 0;for (let i = 1; i <= n; i++) { if (isTidy(lines[i].trim())) { out.push("예"); count++; } else { out.push("아니요"); }}out.push(String(count));console.log(out.join("\n"));
JavaMain.java · 28 줄
import java.io.*;public class Main { // 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 true static boolean isTidy(String s) { for (int i = 0; i + 1 < s.length(); i++) { if (s.charAt(i) > s.charAt(i + 1)) return false; } return true; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringBuilder sb = new StringBuilder(); int count = 0; for (int i = 0; i < n; i++) { if (isTidy(br.readLine().trim())) { sb.append("예\n"); count++; } else { sb.append("아니요\n"); } } sb.append(count).append('\n'); System.out.print(sb); }}
C#Main.cs · 36 줄
using System;using System.Text;class Program{ // 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 true static bool IsTidy(string s) { for (int i = 0; i + 1 < s.Length; i++) { if (s[i] > s[i + 1]) return false; } return true; } static void Main() { int n = int.Parse(Console.ReadLine()!.Trim()); var sb = new StringBuilder(); int count = 0; for (int i = 0; i < n; i++) { if (IsTidy(Console.ReadLine()!.Trim())) { sb.Append("예\n"); count++; } else { sb.Append("아니요\n"); } } sb.Append(count).Append('\n'); Console.Write(sb); }}
C++main.cpp · 27 줄
#include <iostream>#include <string>// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 truebool isTidy(const std::string& s) { for (size_t i = 0; i + 1 < s.size(); i++) { if (s[i] > s[i + 1]) return false; } return true;}int main() { std::ios::sync_with_stdio(false); int n, count = 0; std::string s; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> s; if (isTidy(s)) { std::cout << "예\n"; count++; } else { std::cout << "아니요\n"; } } std::cout << count << "\n";}
Cmain.c · 29 줄
#include <stdbool.h>#include <stdio.h>#include <string.h>// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 truebool is_tidy(const char *s) { size_t len = strlen(s); for (size_t i = 0; i + 1 < len; i++) { if (s[i] > s[i + 1]) return false; } return true;}int main(void) { int n, count = 0; char s[32]; // 18자리까지라 문자열로 읽는다 scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%31s", s); if (is_tidy(s)) { puts("예"); count++; } else { puts("아니요"); } } printf("%d\n", count); return 0;}
Craykonmain.cray · 28 줄
// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 참정리된번호인가(s){ 반복 (i = 0; i + 1 < s.길이; i++) { 만약 (s[i] > s[i + 1]) { 돌려주기 거짓; } } 돌려주기 참; // 끝까지 다 본 뒤에야 참이라고 말할 수 있다}n = 콘솔.줄읽기().수로변환();count = 0;반복 (k = 0; k < n; k++){ 만약 (정리된번호인가(콘솔.줄읽기().공백제거())) { 콘솔.쓰기("예"); count++; } 아니면 { 콘솔.쓰기("아니요"); }}콘솔.쓰기(count);