공 바꾸기 (10813)

Created:

Baekjoon No.10813
i번 바구니와 j번 바구니의 공을 서로 바꿔라.

Bash

Bash
1
2
3
4
5
6
7
8
9
10
read n m
arr=()
for ((i=0; i<=n; i++)); do arr+=($i); done
for ((idx=0; idx<m; idx++)); do
	read i j
	bak=${arr[$i]}
	arr[$i]=${arr[$j]}
	arr[$j]=$bak
done
echo "${arr[@]:1}"

Node.js

JavaScript
1
2
3
4
5
6
7
8
const [[n, m], ...input] = require("fs").readFileSync(0).toString().trim().split("\n").map(x => x.split(" ").map(Number));
let arr = [...Array(n+1).keys()];
for (let [i, j] of input) {
	let bak = arr[i];
	arr[i] = arr[j];
	arr[j] = bak;
}
console.log( arr.slice(1).join(" ") );

Python3

Python
1
2
3
4
5
6
n, m = map(int, input().split())
arr = [i for i in range(n+1)]
for idx in range(m):
    i, j = map(int, input().split())
    arr[i], arr[j] = arr[j], arr[i]
print( ' '.join(map(str, arr[1:])) )

Ruby

Ruby
1
2
3
4
5
6
7
n, m = gets.chomp.split().map {|i| i.to_i}
arr = (0..n).to_a
for idx in 0...m
  i, j = gets.chomp.split().map {|i| i.to_i}
  arr[i], arr[j] = arr[j], arr[i]
end
puts arr[1, n].join(" ")