paizaラーニングレベルアップ問題集の「配列の反転」「配列の順序の反転」をやってみた。

paizaラーニングレベルアップ問題集の「配列の反転」「配列の順序の反転」をやってみました。
- - -
##### 問題
###### 配列の反転
https://paiza.jp/works/mondai/array_primer/array_primer__array_reverse
###### 配列の順序の反転
https://paiza.jp/works/mondai/array_utilization_primer/array_utilization_primer__reverse
- - -
- 1問目は
	- 配列は半角空白区切り（ヨコ入力）で与えられます
	- 今回は、破壊的メソッド（配列自体を書き換える）で実装してみます
- 2問目は
	- 配列は改行区切り（タテ入力）で与えられます
	- 問題文に「$A$の要素の順序を逆にした配列$B$を作成し」とあるので、非破壊的メソッド（新しい配列を作成する）で実装してみます
- - -
##### C
###### 配列の反転
```c
#include <stdio.h>

void reverse(int n, int A[n]) {
	for (int i = 0; i < n + ~i; i++) {
		int tmp = A[i];
		A[i] = A[n+~i];
		A[n+~i] = A[i];
	}
}

int main() {
	int n;
	scanf("%d", &n);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	reverse(n, A);
	for (int i = 0; i < n; i++) printf("%d\n", A[i]);
	return 0;
}
```
###### 配列の順序の反転
```c
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	int B[n];
	for (int i = 0; i < n; i++) B[i] = A[n+~i];
	for (int i = 0; i < n; i++) printf("%d\n", B[i]);
	return 0;
}
```
- - -
##### C++
###### 配列の反転
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	reverse(A.begin(), A.end());
	for (int i = 0; i < n; i++) cout << A[i] << endl;
	return 0;
}
```
###### 配列の順序の反転
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	vector<int> B(A.rbegin(), A.rend());
	for (int i = 0; i < n; i++) cout << B[i] << endl;
	return 0;
}
```
- - -
##### C#
###### 配列の反転
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Array.Reverse(A);
		for (int i = 0; i < n; i++) Console.WriteLine(A[i]);
	}
}
```
###### 配列の順序の反転
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = new int[n];
		for (int i = 0; i < n; i++) A[i] = int.Parse(Console.ReadLine());
		int[] B = new int[n];
		for (int i = 0; i < n; i++) B[i] = A[n+~i];
		for (int i = 0; i < n; i++) Console.WriteLine(B[i]);
	}
}
```
- - -
##### Go
###### 配列の反転
```go
package main
import "fmt"

func reverse(A []int) {
	n := len(A)
	for i := 0; i < n + ^i; i++ {
		tmp := A[i]
		A[i] = A[n+^i]
		A[n+^i] = tmp
	}
}

func main() {
	var n int
	fmt.Scan(&n)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	reverse(A)
	for i := 0; i < n; i++ {
		fmt.Println(A[i])
	}
}
```
###### 配列の順序の反転
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	B := make([]int, n)
	for i := 0; i < n; i++ {
		B[i] = A[n+^i]
	}
	for i := 0; i < n; i++ {
		fmt.Println(B[i])
	}
}
```
- - -
##### Java
###### 配列の反転
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Integer[] A = new Integer[n];
		for (int i = 0; i < n; i++) A[i] = sc.nextInt();
		sc.close();
		Collections.reverse(Arrays.asList(A));
		for (int i = 0; i < n; i++) System.out.println(A[i]);
	}
}
```
###### 配列の順序の反転
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] A = new int[n];
		for (int i = 0; i < n; i++) A[i] = sc.nextInt();
		sc.close();
		int[] B = new int[n];
		for (int i = 0; i < n; i++) B[i] = A[n+~i];
		for (int i = 0; i < n; i++) System.out.println(B[i]);
	}
}
```
- - -
##### JavaScript
###### 配列の反転
```js
const [n, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
A.reverse();
for (var i = 0; i < n; i++) console.log(A[i]);
```
###### 配列の順序の反転
```js
const [n, ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(Number);
const B = A.slice().reverse();
for (var i = 0; i < n; i++) console.log(B[i]);
```
- - -
##### Kotlin
###### 配列の反転
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }.toTypedArray()
	A.reverse()
	for (i in 0 until n) println(A[i])
}
```
###### 配列の順序の反転
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = Array(n) { readLine()!!.toInt() }
	val B = (1..n).map { A[n-it] }
	for (i in 0 until n) println(B[i])
}
```
- - -
##### PHP
###### 配列の反転
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	for ($i = 0; $i < $n+~$i; $i++) {
		$tmp = $A[$i];
		$A[$i] = $A[$n+~$i];
		$A[$n+~$i] = $tmp;
	}
	for ($i = 0; $i < $n; $i++)	echo $A[$i], PHP_EOL;
?>
```
###### 配列の順序の反転
```php
<?php
	$n = intval(fgets(STDIN));
	$A = [];
	for ($i = 0; $i < $n; $i++)	$A[] = intval(fgets(STDIN));
	$B = array_reverse($A);
	for ($i = 0; $i < $n; $i++)	echo $B[$i], PHP_EOL;
?>
```
- - -
##### Perl
###### 配列の反転
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
for (my $i = 0; $i < $n - $i - 1; $i++) {
	my $tmp = $A[$i];
	$A[$i] = $A[$n-$i-1];
	$A[$n-$i-1] = $tmp;
}
for (my $i = 0; $i < $n; $i++) {
	print "$A[$i]$/";
}
```
###### 配列の順序の反転
```pl
my $n = int(<STDIN>);
my @A;
for (my $i = 0; $i < $n; $i++) {
	push @A, int(<STDIN>);
}
my @B = reverse @A;
for (my $i = 0; $i < $n; $i++) {
	print "$B[$i]$/";
}
```
- - -
##### Python3
###### 配列の反転
```py
n = int(input())
A = list(map(int, input().split()))
A.reverse()
for i in range(n):
	print(A[i])
```
###### 配列の順序の反転
```py
n = int(input())
A = [int(input()) for _ in range(n)]
B = list(reversed(A))
for i in range(n):
	print(B[i])
```
- - -
##### Ruby
###### 配列の反転
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
A.reverse!
N.times do |i|
	p A[i]
end
```
###### 配列の順序の反転
```rb
N = gets.to_i
A = N.times.map { gets.to_i }
B = A.reverse
N.times do |i|
	p B[i]
end
```
- - -
##### Scala
###### 配列の反転
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	val B = A.reverse
	for (i <- 0 until n) println(B(i))
}
```
###### 配列の順序の反転
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = (0 until n).map { _ => readInt() }
	val B = A.reverse
	for (i <- 0 until n) println(B(i))
}
```
- - -
##### Swift
###### 配列の反転
```swift
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.reverse()
for i in 0..<n {
	print(A[i])
}
```
###### 配列の順序の反転
```swift
let n = Int(readLine()!)!
var A: [Int] = []
for _ in 0..<n {
	A.append(Int(readLine()!)!)
}
let B = (1...n).map { A[n-$0] }
for i in 0..<n {
	print(B[i])
}
```