paizaラーニングレベルアップ問題集の【配列メニュー】をやってみた。(12)

paizaラーニングレベルアップ問題集の【配列メニュー】をやってみました。
- - -
##### 問題
###### 配列のソート
https://paiza.jp/works/mondai/array_primer/array_primer__array_sort
###### 配列の反転
https://paiza.jp/works/mondai/array_primer/array_primer__array_reverse
- - -
##### C
###### 配列のソート
```c
#include <stdio.h>
#include <stdlib.h>

int cmp(const void *p, const void *q) {
	int x = *(int*) p;
	int y = *(int*) q;
	if (x < y) return -1;
	if (x > y) return 1;
	return 0;
}

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

int *reverse(int n, int *A) {
	for (int i = 0, j = n - 1; i < j; i++, j--) {
		int t = A[i];
		A[i] = A[j];
		A[j] = t;
	}
	return A;
}

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++
###### 配列のソート
```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];
	sort(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];
	reverse(A.begin(), A.end());
	for (int i = 0; i < n; i++) cout << A[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.Sort(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 = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Array.Reverse(A);
		for (int i = 0; i < n; i++) Console.WriteLine(A[i]);
	}
}
```
- - -
##### Go
###### 配列のソート
```go
package main
import (
	"fmt"
	"sort"
)

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

func reverse(A []int) []int {
	for i, j := 0, len(A)-1; i < j; i, j = i+1, j-1 {
		A[i], A[j] = A[j], A[i]
	}
	return A
}

func main() {
	var n int
	fmt.Scan(&n)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	A = reverse(A)
	for i := 0; i < n; i++ {
		fmt.Println(A[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();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		Arrays.sort(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();
		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]);
	}
}
```
- - -
##### 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.sort();
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((s, i) => i ? s.split(' ').map(Number) : Number(s));
A.reverse();
for (var i = 0; i < n; i++) console.log(A[i]);
```
- - -
##### Kotlin
###### 配列のソート
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	var A = readLine()!!.split(' ').map { it.toInt() }.toTypedArray()
	A.sort()
	for (i in 0 until n) println(A[i])
}
```
###### 配列の反転
```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])
}
```
- - -
##### PHP
###### 配列のソート
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	sort($A);
	for ($i = 0; $i < $n; $i++)	echo $A[$i], PHP_EOL;
?>
```
###### 配列の反転
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', 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>;
my @B = sort @A;
foreach $b (@B) {
	print "$b$/";
}
```
###### 配列の反転
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my @B = reverse @A;
foreach $b (@B) {
	print "$b$/";
}
```
- - -
##### Python3
###### 配列のソート
```py
n = int(input())
A = list(map(int, input().split()))
A.sort()
for i in range(n):
	print(A[i])
```
```py
n = int(input())
A = list(map(int, input().split()))
B = sorted(A)
for i in range(n):
	print(B[i])
```
###### 配列の反転
```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 = list(map(int, input().split()))
B = list(reversed(A))
for i in range(n):
	print(B[i])
```
- - -
##### Ruby
###### 配列のソート
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
B = A.sort
N.times do |i|
	p A[i]
end
```
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
A.sort!
N.times do |i|
	p A[i]
end
```
###### 配列の反転
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
B = A.reverse
N.times do |i|
	p B[i]
end
```
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
A.reverse!
N.times do |i|
	p A[i]
end
```
- - -
##### Scala
###### 配列のソート
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	val B = A.sorted
	for (i <- 0 until n) println(B(i))
}
```
###### 配列の反転
```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))
}
```
- - -
##### Swift
###### 配列のソート
```swift
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.sort()
for i in 0..<n {
	print(A[i])
}
```
###### 配列の反転
```swift
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.reverse()
for i in 0..<n {
	print(A[i])
}
```