n = int(input())words = input().split()# sorted는 안정 정렬이라, 소문자로 같은 단어끼리는 들어온 차례를 지킨다print(" ".join(sorted(words, key=str.lower)))
JavaScriptmain.js · 9 行
const [first, second] = require("fs").readFileSync(0, "utf8").split("\n");const words = second.trim().split(" ");const byLower = (a, b) => { const x = a.toLowerCase(); const y = b.toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; // < 는 글자 코드 순(localeCompare 아님)};// Array.prototype.sort는 ES2019부터 안정 정렬이다console.log(words.sort(byLower).join(" "));
TypeScriptmain.ts · 10 行
import { readFileSync } from "node:fs";const [, second] = readFileSync(0, "utf8").split("\n");const words: string[] = second.trim().split(" ");const byLower = (a: string, b: string): number => { const x: string = a.toLowerCase(); const y: string = b.toLowerCase(); return x < y ? -1 : x > y ? 1 : 0; // < 는 글자 코드 순(localeCompare 아님)};// Array.prototype.sort는 ES2019부터 안정 정렬이다console.log(words.sort(byLower).join(" "));
JavaMain.java · 18 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.Comparator;import java.util.List;import java.util.Locale;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in.readLine(); List<String> words = Arrays.asList(in.readLine().trim().split(" ")); // List.sort(객체)는 안정 정렬(TimSort). 소문자로 바꾼 것을 글자 코드로 비교한다 words.sort(Comparator.comparing((String w) -> w.toLowerCase(Locale.ROOT))); System.out.println(String.join(" ", words)); }}
C#Main.cs · 14 行
using System;using System.Linq;class Program{ static void Main() { Console.ReadLine(); string[] words = Console.ReadLine().Trim().Split(' '); // OrderBy는 안정 정렬. Ordinal은 글자 코드 순(문화권 규칙 없이) var sorted = words.OrderBy(w => w.ToLowerInvariant(), StringComparer.Ordinal); Console.WriteLine(string.Join(" ", sorted)); }}
C++main.cpp · 22 行
#include <algorithm>#include <cctype>#include <iostream>#include <string>#include <vector>static std::string lower(std::string s) { for (char& c : s) c = (char)std::tolower((unsigned char)c); return s;}int main() { int n; std::cin >> n; std::vector<std::string> words(n); for (auto& w : words) std::cin >> w; // std::sort는 안정 정렬이 아니다: 같은 것끼리 차례를 지키려면 stable_sort std::stable_sort(words.begin(), words.end(), [](const std::string& a, const std::string& b) { return lower(a) < lower(b); }); for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << words[i]; std::cout << '\n';}
Cmain.c · 31 行
#include <ctype.h>#include <stdio.h>#include <stdlib.h>struct Word { char text[21]; int order; /* 들어온 차례: qsort는 안정 정렬이 아니라서 직접 비교에 넣는다 */};static int by_lower(const void *pa, const void *pb) { const struct Word *a = pa, *b = pb; const unsigned char *x = (const unsigned char *)a->text, *y = (const unsigned char *)b->text; while (*x && tolower(*x) == tolower(*y)) x++, y++; int d = tolower(*x) - tolower(*y); /* 한쪽이 끝나면 '\0'(0)이라 짧은 쪽이 앞 */ if (d != 0) return d; return a->order - b->order;}int main(void) { static struct Word w[2000]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%20s", w[i].text); w[i].order = i; } qsort(w, n, sizeof w[0], by_lower); for (int i = 0; i < n; i++) printf(i ? " %s" : "%s", w[i].text); printf("\n"); return 0;}
Craykonmain.cray · 21 行
n = コンソール.行読み().数に();words = コンソール.行読み().分ける();// 정렬(기준함수)는 같은 것끼리 들어온 차례를 지킨다words.並べ替え(byLower);コンソール.書く(words.つなぐ(" "));// 소문자로 바꾼 것을 글자 코드 순으로 비교byLower(앞, 뒤){ a = 앞.小文字(); b = 뒤.小文字(); もし (a < b) { 返す -1; } もし (a > b) { 返す 1; } 返す 0;}