Arrange n integers in two different orders.
The first is number order: from the smallest number up.
The second is dictionary order: treat each number as the string exactly as written in the input and compare the strings.
Two strings are compared one character at a time from the front, the one whose first differing character comes earlier goes first, and the order of characters is -, 0, 1, …, 9.
If one string is exactly the beginning of the other, the shorter one goes first.
The first line gives n, and the second line gives the n integers separated by single spaces.
Print the n numbers in number order on the first line and in dictionary order on the second line, separated by single spaces.
Print equal numbers as many times as they appear.
On the third line, print how many positions hold the same number in both lines.
Input
1 ≤ n ≤ 1,000. Each integer is from -2,000,000,000 to 2,000,000,000 and is given as digits only, without commas. A negative number starts with -; there is never a + sign or an unnecessary zero (007, -0)
Output
Three lines: number order, dictionary order, and the count of positions holding the same number in both
Example 1 · stdin
6
10 9 -3 100 -20 9
stdout
-20 -3 9 9 10 100
-20 -3 10 100 9 9
2
Example 2 · stdin
2
2000000000 -2000000000
stdout
-2000000000 2000000000
-2000000000 2000000000
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
The same sorting tool chooses a different order depending on what it holds. Python sorted(tokens, key=int) compares by the values turned into numbers, and sorted(tokens) compares the strings as they are. Java Arrays.sort gives number order for int[] and dictionary order for String[], and C++ std::sort differs the same way between vector<int> and vector<string>. Craykon list.Sort() also gives number order for a list of numbers and dictionary order for a list of strings. C qsort always needs a comparing function, so use (x > y) - (x < y) for numbers and strcmp for strings. In C# use Array.Sort(byNum) and Array.Sort(byText, StringComparer.Ordinal), telling it to compare strings by character code (the default comparison follows culture rules; with only - and digits the two give the same result on this judge, but it is safer to say you want character-code order). Only JS/TS is the other way round: sort() with no argument is dictionary order, and for number order you must pass a comparing function, sort((x, y) => x - y). Four traps: (1) If you do not convert to numbers or do not say how to compare, the first line comes out in dictionary order (the first line of example 1 becomes -20 -3 10 100 9 9). JS nums.sort() converts to strings to compare even for an array of numbers; leaving out key=int in Python, or calling Sort() on a list of strings without ToNumber in Craykon, does the same. (2) Sorting in place does not make a copy. JS sort sorts that array and returns the very same one, so const byNum = nums.sort(...) and const byText = nums.sort() are one array. Python by_num = tokens is not a copy either. The two lines become identical and the third line becomes n (6 in example 1). Make separate ones with JS [...tokens], Python sorted, Java clone(), Craykon GetRange(0, n). (3) Python by_num = tokens.sort(key=int) stores None, not the sorted list, and the line that uses it stops with a TypeError. (4) A comparing function written as a subtraction, C return x - y; or Java (x, y) -> x - y, overflows int on 2000000000 - (-2000000000) and turns negative, so the big number is placed first (the first line of example 2 becomes 2000000000 -2000000000). Use (x > y) - (x < y) in C and Integer.compare(x, y) in Java. In JS x - y does not overflow in this range because numbers are floating point.
Pythonmain.py · 12 lines
n = int(input())tokens = input().split()# sorted는 새 목록을 돌려주고 원래 목록은 그대로 둔다by_num = sorted(tokens, key=int) # 수로 바꾼 값을 기준으로: 수 크기 순서by_text = sorted(tokens) # 문자열 그대로: 사전 순서same = sum(1 for x, y in zip(by_num, by_text) if x == y)print(*by_num)print(*by_text)print(same)
JavaScriptmain.js · 18 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const tokens = lines[1].trim().split(/\s+/);// sort는 그 배열 자체를 정렬하므로, 순서마다 배열을 따로 만든다// 수 크기 순서는 견주는 함수를 꼭 넘겨야 한다const byNum = tokens.map(Number).sort((x, y) => x - y);// 아무것도 넘기지 않은 sort()는 문자열로 견준다: 사전 순서const byText = [...tokens].sort();let same = 0;for (let i = 0; i < n; i++) { if (byNum[i] === Number(byText[i])) same++;}console.log(byNum.join(" "));console.log(byText.join(" "));console.log(same);
TypeScriptmain.ts · 19 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const tokens: string[] = lines[1].trim().split(/\s+/);// sort는 그 배열 자체를 정렬하므로, 순서마다 배열을 따로 만든다// 수 크기 순서는 견주는 함수를 꼭 넘겨야 한다const byNum: number[] = tokens.map(Number).sort((x: number, y: number) => x - y);// 아무것도 넘기지 않은 sort()는 문자열로 견준다: 사전 순서const byText: string[] = [...tokens].sort();let same: number = 0;for (let i = 0; i < n; i++) { if (byNum[i] === Number(byText[i])) same++;}console.log(byNum.join(" "));console.log(byText.join(" "));console.log(same);
JavaMain.java · 37 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine().trim()); String[] tokens = in.readLine().trim().split(" +"); // int 배열을 정렬하면 수 크기 순서 int[] byNum = new int[n]; for (int i = 0; i < n; i++) byNum[i] = Integer.parseInt(tokens[i]); Arrays.sort(byNum); // String 배열을 정렬하면 사전 순서. clone으로 복사본을 만들어 정렬한다 String[] byText = tokens.clone(); Arrays.sort(byText); int same = 0; StringBuilder numLine = new StringBuilder(); StringBuilder textLine = new StringBuilder(); for (int i = 0; i < n; i++) { if (byNum[i] == Integer.parseInt(byText[i])) same++; if (i > 0) { numLine.append(' '); textLine.append(' '); } numLine.append(byNum[i]); textLine.append(byText[i]); } System.out.println(numLine); System.out.println(textLine); System.out.println(same); }}
C#Main.cs · 28 lines
using System;class Program{ static void Main() { int n = int.Parse(Console.ReadLine().Trim()); string[] tokens = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); // int 배열을 정렬하면 수 크기 순서 int[] byNum = Array.ConvertAll(tokens, int.Parse); Array.Sort(byNum); // 문자열은 StringComparer.Ordinal을 넘겨 글자 코드 순서(사전 순서)로 정렬한다 string[] byText = (string[])tokens.Clone(); Array.Sort(byText, StringComparer.Ordinal); int same = 0; for (int i = 0; i < n; i++) { if (byNum[i] == int.Parse(byText[i])) same++; } Console.WriteLine(string.Join(" ", byNum)); Console.WriteLine(string.Join(" ", byText)); Console.WriteLine(same); }}
C++main.cpp · 28 lines
#include <algorithm>#include <iostream>#include <string>#include <vector>int main() { int n; std::cin >> n; std::vector<std::string> byText(n); std::vector<int> byNum(n); for (int i = 0; i < n; i++) { std::cin >> byText[i]; byNum[i] = std::stoi(byText[i]); } // 같은 std::sort지만 int는 수 크기로, std::string은 사전 순서로 견준다 std::sort(byNum.begin(), byNum.end()); std::sort(byText.begin(), byText.end()); int same = 0; for (int i = 0; i < n; i++) { if (byNum[i] == std::stoi(byText[i])) same++; } for (int i = 0; i < n; i++) std::cout << byNum[i] << (i + 1 < n ? ' ' : '\n'); for (int i = 0; i < n; i++) std::cout << byText[i] << (i + 1 < n ? ' ' : '\n'); std::cout << same << '\n';}
Cmain.c · 39 lines
#include <stdio.h>#include <stdlib.h>#include <string.h>/* 수 크기로 견준다. 빼기(x - y)는 int 범위를 넘칠 수 있어서 쓰지 않는다 */static int by_num(const void *a, const void *b) { int x = *(const int *)a, y = *(const int *)b; return (x > y) - (x < y);}/* 문자열로 견준다. strcmp가 바로 사전 순서다 */static int by_text(const void *a, const void *b) { return strcmp((const char *)a, (const char *)b);}int main(void) { int n; static int nums[1000]; static char texts[1000][12]; /* "-2000000000"은 11글자, 끝 표시까지 12칸 */ scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%11s", texts[i]); nums[i] = atoi(texts[i]); } /* qsort는 견주는 함수를 받는다. 배열마다 다른 함수를 넘긴다 */ qsort(nums, n, sizeof nums[0], by_num); qsort(texts, n, sizeof texts[0], by_text); int same = 0; for (int i = 0; i < n; i++) { if (nums[i] == atoi(texts[i])) same++; } for (int i = 0; i < n; i++) printf("%d%c", nums[i], i + 1 < n ? ' ' : '\n'); for (int i = 0; i < n; i++) printf("%s%c", texts[i], i + 1 < n ? ' ' : '\n'); printf("%d\n", same); return 0;}
Craykonmain.cray · 27 lines
n = Console.ReadLine().ToNumber();tokens = Console.ReadLine().Trim().Split();// 정렬()은 그 목록 자체를 정렬한다. 수 목록은 수 크기 순서가 된다byNum = [];foreach (t in tokens){ byNum.Add(t.ToNumber());}byNum.Sort();// 문자열 목록은 사전 순서가 된다. 부분목록으로 복사본을 만들어 정렬한다byText = tokens.GetRange(0, n);byText.Sort();same = 0;for (i = 0; i < n; i++){ if (byNum[i] == byText[i].ToNumber()) { same++; }}Console.WriteLine(byNum.Join(" "));Console.WriteLine(byText.Join(" "));Console.WriteLine(same);