In gym class n students stand in a line, and the positions are numbered 1, 2, …, n from the left.
The first line gives n and s separated by one space.
The second line gives the students' jersey numbers in position order, separated by single spaces.
When the teacher blows the whistle, the students pair up two by two going right, starting from position s.
That is, positions s and s+1 are a pair, positions s+2 and s+3 are a pair, and so on to the end of the line.
The two students of each pair swap places.
A student without a partner stays where they are.
The students without a partner are the one at position 1 when s is 2, and the one left alone at the end of the line.
Print the jersey numbers after the swaps in position order on one line, separated by single spaces.
Input
1 ≤ n ≤ 100, s is 1 or 2, 1 ≤ jersey number ≤ 999 (integers), all jersey numbers are different
Output
One line with the n jersey numbers separated by single spaces
Example 1 · stdin
5 1
10 20 30 40 50
stdout
20 10 40 30 50
Example 2 · stdin
5 2
10 20 30 40 50
stdout
10 30 20 50 40
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
How you swap two slots differs by language. Python a[i], a[i + 1] = a[i + 1], a[i], JS/TS [a[i], a[i + 1]] = [a[i + 1], a[i]], C# (a[i], a[i + 1]) = (a[i + 1], a[i]) and C++ std::swap(a[i], a[i + 1]) do it in one line. C, Java and Craykon park one side in a temporary variable and use three lines. The first trap is writing a[i] = a[i + 1]; a[i + 1] = a[i]; without a temporary. The first statement destroys the old value of a[i], so example 1 becomes 20 20 40 40 50. The second trap is where the loop ends. The right-hand slot i + 1 of a pair must be inside the list, so the condition is i + 1 < n. With i < n the last student, who has no partner, is swapped with a slot outside the list. On example 1 Python stops with IndexError and Craykon stops saying the list only has slots 0 to 4. JS raises no error: it swaps with the undefined of the missing slot, the list grows by one, and you get 20 10 40 30, then two spaces, then 50. C and C++ do no range check, so they can silently read some unrelated value. The third trap is numbering. Position s counts from 1 while list indexes count from 0, so start at index s − 1. Using s as it is shifts everything by one, and ignoring s and always starting at the front turns example 2 into 20 10 40 30 50.
Pythonmain.py · 6 lines
n, s = map(int, input().split())a = list(map(int, input().split()))# 자리 번호 s는 1부터 세므로 목록에서는 s - 1번 칸이다for i in range(s - 1, n - 1, 2): a[i], a[i + 1] = a[i + 1], a[i] # 임시 변수 없이 한 줄로 맞바꾼다print(*a)
JavaScriptmain.js · 6 lines
const [n, s, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// 짝의 오른쪽 칸(i + 1)이 목록 안에 있을 때만 바꾼다for (let i = s - 1; i + 1 < n; i += 2) { [a[i], a[i + 1]] = [a[i + 1], a[i]];}console.log(a.join(" "));
TypeScriptmain.ts · 7 lines
import { readFileSync } from "node:fs";const [n, s, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// 짝의 오른쪽 칸(i + 1)이 목록 안에 있을 때만 바꾼다for (let i = s - 1; i + 1 < n; i += 2) { [a[i], a[i + 1]] = [a[i + 1], a[i]];}console.log(a.join(" "));
JavaMain.java · 22 lines
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int s = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); for (int i = s - 1; i + 1 < n; i += 2) { int tmp = a[i]; // 한쪽을 먼저 맡겨 둔다 a[i] = a[i + 1]; a[i + 1] = tmp; } StringBuilder out = new StringBuilder(); for (int i = 0; i < n; i++) { if (i > 0) out.append(' '); out.append(a[i]); } System.out.println(out); }}
C#Main.cs · 17 lines
using System;class Program{ static void Main() { string[] first = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); int n = int.Parse(first[0]); int s = int.Parse(first[1]); int[] a = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); for (int i = s - 1; i + 1 < n; i += 2) { (a[i], a[i + 1]) = (a[i + 1], a[i]); // 튜플로 한 줄에 맞바꾼다 } Console.WriteLine(string.Join(" ", a)); }}
C++main.cpp · 15 lines
#include <iostream>#include <utility>#include <vector>int main() { int n, s; std::cin >> n >> s; std::vector<int> a(n); for (int& x : a) std::cin >> x; for (int i = s - 1; i + 1 < n; i += 2) { std::swap(a[i], a[i + 1]); } for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << a[i]; std::cout << "\n";}
Cmain.c · 15 lines
#include <stdio.h>int main(void) { int n, s, a[100]; scanf("%d %d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = s - 1; i + 1 < n; i += 2) { int tmp = a[i]; /* 한쪽을 먼저 맡겨 둔다 */ a[i] = a[i + 1]; a[i + 1] = tmp; } for (int i = 0; i < n; i++) printf(i ? " %d" : "%d", a[i]); printf("\n"); return 0;}
Craykonmain.cray · 12 lines
첫줄 = Console.ReadNumbers();n = 첫줄[0];s = 첫줄[1];a = Console.ReadNumbers();for (i = s - 1; i + 1 < n; i += 2){ // 한쪽을 먼저 임시로 맡겨 둔다 임시 = a[i]; a[i] = a[i + 1]; a[i + 1] = 임시;}Console.WriteLine(a);