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ラーニングレベルアップ問題集の「配列の反転」「配列の順序の反転」をやってみた。

Posted at

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


問題
配列の反転

配列の順序の反転


  • 1問目は
    • 配列は半角空白区切り(ヨコ入力)で与えられます
    • 今回は、破壊的メソッド(配列自体を書き換える)で実装してみます
  • 2問目は
    • 配列は改行区切り(タテ入力)で与えられます
    • 問題文に「$A$の要素の順序を逆にした配列$B$を作成し」とあるので、非破壊的メソッド(新しい配列を作成する)で実装してみます

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;
}
配列の順序の反転
#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++
配列の反転
#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;
}
配列の順序の反転
#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#
配列の反転
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]);
	}
}
配列の順序の反転
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
配列の反転
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])
	}
}
配列の順序の反転
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
配列の反転
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]);
	}
}
配列の順序の反転
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
配列の反転
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]);
配列の順序の反転
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
配列の反転
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])
}
配列の順序の反転
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
	$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
	$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
配列の反転
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]$/";
}
配列の順序の反転
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
配列の反転
n = int(input())
A = list(map(int, input().split()))
A.reverse()
for i in range(n):
	print(A[i])
配列の順序の反転
n = int(input())
A = [int(input()) for _ in range(n)]
B = list(reversed(A))
for i in range(n):
	print(B[i])

Ruby
配列の反転
N = gets.to_i
A = gets.split.map(&:to_i)
A.reverse!
N.times do |i|
	p A[i]
end
配列の順序の反転
N = gets.to_i
A = N.times.map { gets.to_i }
B = A.reverse
N.times do |i|
	p B[i]
end

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