0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

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


問題
配列のソート

配列の反転


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;
}
配列の反転
#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++
配列のソート
#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;
}
配列の反転
#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#
配列のソート
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]);
	}
}
配列の反転
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
配列のソート
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])
	}
}
配列の反転
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
配列のソート
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]);
	}
}
配列の反転
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
配列のソート
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]);
配列の反転
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
配列のソート
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])
}
配列の反転
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
	$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
	$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
配列のソート
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my @B = sort @A;
foreach $b (@B) {
	print "$b$/";
}
配列の反転
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my @B = reverse @A;
foreach $b (@B) {
	print "$b$/";
}

Python3
配列のソート
n = int(input())
A = list(map(int, input().split()))
A.sort()
for i in range(n):
	print(A[i])
n = int(input())
A = list(map(int, input().split()))
B = sorted(A)
for i in range(n):
	print(B[i])
配列の反転
n = int(input())
A = list(map(int, input().split()))
A.reverse()
for i in range(n):
	print(A[i])
n = int(input())
A = list(map(int, input().split()))
B = list(reversed(A))
for i in range(n):
	print(B[i])

Ruby
配列のソート
N = gets.to_i
A = gets.split.map(&:to_i)
B = A.sort
N.times do |i|
	p A[i]
end
N = gets.to_i
A = gets.split.map(&:to_i)
A.sort!
N.times do |i|
	p A[i]
end
配列の反転
N = gets.to_i
A = gets.split.map(&:to_i)
B = A.reverse
N.times do |i|
	p B[i]
end
N = gets.to_i
A = gets.split.map(&:to_i)
A.reverse!
N.times do |i|
	p A[i]
end

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))
}
配列の反転
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
配列のソート
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.sort()
for i in 0..<n {
	print(A[i])
}
配列の反転
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.reverse()
for i in 0..<n {
	print(A[i])
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?