A school that loves order hands out lockers with "tidy numbers" first.
A number is tidy if, reading from the left, every digit is greater than or equal to the digit just to its left.
For example, 1123, 7 and 0 are tidy, while 1231 (a 1 after the 3) and 10 are not.
Every one-digit number is tidy.
The first line gives the number of lockers n, and the next n lines give one number each.
For each number print "yes" if it is tidy and "no" otherwise, one per line.
On the last line print how many numbers are tidy.
Write a function that takes one number and returns true or false for whether it is tidy, and use it.
Input
1 ≤ n ≤ 1,000. 0 ≤ number < 10^18 (at most 18 digits); unless the number is 0, its first digit is not 0.
Output
n+1 lines: yes or no for each number, then the count of yes
Example 1 · stdin
4
1123
1231
7
10
stdout
yes
no
yes
no
2
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
A function that returns true or false is written as "return false as soon as you find one bad spot; return true only after checking everything". The boolean type has different names: Python bool (True/False), JS and TS boolean, Java boolean, C# bool, C++ bool, C uses bool from <stdbool.h> (older C returned 0 or 1 as an int), and Craykon returns the values true/false with return. In Python it also fits on one line: all(a <= b for a, b in zip(s, s[1:])). Digit characters are ordered by character code the same way as the digits, so you can compare s[i] > s[i + 1] directly without converting to numbers. Four traps: ① Inside the loop, following if bad: return False with else: return True returns true after looking at only the first pair (1231 becomes yes). ② Repeated digits (112) are still tidy. It is false only when a digit gets smaller, so ask >, not >=. ③ In JS, return false inside forEach ends only that one callback, not the outer function, so the function always returns true. To leave early use a for loop or every (in Java and C# lambdas, return false does not even compile). ④ Numbers have up to 18 digits, which do not fit in an int (about 2.1 billion). Java Integer.parseInt throws and C scanf("%d") overflows. Reading them as strings avoids the size problem.
Pythonmain.py · 17 lines
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("yes") count += 1 else: print("no")print(count)
JavaScriptmain.js · 23 lines
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("yes"); count++; } else { out.push("no"); }}out.push(count);console.log(out.join("\n"));
TypeScriptmain.ts · 25 lines
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("yes"); count++; } else { out.push("no"); }}out.push(String(count));console.log(out.join("\n"));
JavaMain.java · 28 lines
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("yes\n"); count++; } else { sb.append("no\n"); } } sb.append(count).append('\n'); System.out.print(sb); }}
C#Main.cs · 36 lines
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("yes\n"); count++; } else { sb.Append("no\n"); } } sb.Append(count).Append('\n'); Console.Write(sb); }}
C++main.cpp · 27 lines
#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 << "yes\n"; count++; } else { std::cout << "no\n"; } } std::cout << count << "\n";}
Cmain.c · 29 lines
#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("yes"); count++; } else { puts("no"); } } printf("%d\n", count); return 0;}
Craykonmain.cray · 28 lines
// 왼쪽에서 오른쪽으로 숫자가 작아지는 곳이 한 번도 없으면 참정리된번호인가(s){ for (i = 0; i + 1 < s.Length; i++) { if (s[i] > s[i + 1]) { return false; } } return true; // 끝까지 다 본 뒤에야 참이라고 말할 수 있다}n = Console.ReadLine().ToNumber();count = 0;for (k = 0; k < n; k++){ if (정리된번호인가(Console.ReadLine().Trim())) { Console.WriteLine("yes"); count++; } else { Console.WriteLine("no"); }}Console.WriteLine(count);