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

paizaラーニングレベルアップ問題集の【配列メニュー】をやってみました。
- - -
##### 問題
###### 配列末尾への追加
https://paiza.jp/works/mondai/array_primer/array_primer__push_back
###### 要素の削除
https://paiza.jp/works/mondai/array_primer/array_primer__elm_erase
###### 要素の挿入
https://paiza.jp/works/mondai/array_primer/array_primer__elm_insert
- - -
「配列メニュー」と言いながら、今回実際に使用しているのは動的配列、可変長配列やリストです。
- - -
##### C
###### 配列末尾への追加
```c
#include <stdio.h>
#include <stdlib.h>

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	int *A = (int*) malloc(sizeof(int) * n);
	if (!A) abort();
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	A = (int*) realloc(A, sizeof(int) * (n + 1));
	if (!A) abort();
	A[n] = m;
	for (int i = 0; i < n + 1; i++) printf("%d\n", A[i]);
	free(A);
	return 0;
}
```
###### 要素の削除
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	int *A = (int*) malloc(sizeof(int) * n);
	if (!A) abort();
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	memmove(A + m - 1, A + m, sizeof(int) * (n - m));
	A = (int*) realloc(A, sizeof(int) * (n - 1));
	if (!A) abort();
	for (int i = 0; i < n - 1; i++) printf("%d\n", A[i]);
	free(A);
	return 0;
}
```
###### 要素の挿入
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	int n, m, k;
	scanf("%d %d %d", &n, &m, &k);
	m -= 1;
	int *A = (int*) malloc(sizeof(int) * n);
	if (!A) abort();
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	A = (int*) realloc(A, sizeof(int) * (n + 1));
	if (!A) abort();
	memmove(A + m + 1, A + m, sizeof(int) * (n - m));
	A[m] = k;
	for (int i = 0; i < n + 1; i++) printf("%d\n", A[i]);
	free(A);
	return 0;
}
```
- - -
##### C++
###### 配列末尾への追加
```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	A.push_back(m);
	for (int a : A) cout << a << endl;
	return 0;
}
```
###### 要素の削除
```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	m -= 1;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	A.erase(A.begin() + m);
	for (int a : A) cout << a << endl;
	return 0;
}
```
###### 要素の挿入
```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, m, k;
	cin >> n >> m >> k;
	m -= 1;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	A.insert(A.begin() + m, k);
	for (int a : A) cout << a << endl;
	return 0;
}
```
- - -
##### C#
###### 配列末尾への追加
```cs
using System;
using System.Collections.Generic;

class Program
{
	public static void Main()
	{
		int[] nm = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int m = nm[1];
		List<int> A = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse));
		A.Add(m);
		foreach (int a in A) Console.WriteLine(a);
	}
}
```
###### 要素の削除
```cs
using System;
using System.Collections.Generic;

class Program
{
	public static void Main()
	{
		int[] nm = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int m = nm[1] - 1;
		List<int> A = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse));
		A.RemoveAt(m);
		foreach (int a in A) Console.WriteLine(a);
	}
}
```
###### 要素の挿入
```cs
using System;
using System.Collections.Generic;

class Program
{
	public static void Main()
	{
		int[] nmk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int m = nmk[1] - 1, k = nmk[2];
		List<int> A = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse));
		A.Insert(m, k);
		foreach (int a in A) Console.WriteLine(a);
	}
}
```
- - -
##### Go
###### 配列末尾への追加
```go
package main
import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	A = append(A, m)
	for _, a := range(A) {
		fmt.Println(a)
	}
}
```
###### 要素の削除
```go
package main
import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)
	m -= 1 
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	A = append(A[:m], A[m+1:]...)
	for _, a := range(A) {
		fmt.Println(a)
	}
}
```
###### 要素の挿入
```go
package main
import "fmt"

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	m -= 1 
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	A = append(A[:m], append([]int{k}, A[m:]...)...)
	for _, a := range(A) {
		fmt.Println(a)
	}
}
```
- - -
##### Java
###### 配列末尾への追加
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		sc.nextLine();
		List<Integer> A = new ArrayList<>(Arrays.stream(sc.nextLine().split(" ")).map(Integer::valueOf).toList());
		sc.close();
		A.add(m);
		for (int a : A) System.out.println(a);
	}
}
```
###### 要素の削除
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt() - 1;
		sc.nextLine();
		List<Integer> A = new ArrayList<>(Arrays.stream(sc.nextLine().split(" ")).map(Integer::valueOf).toList());
		sc.close();
		A.remove(m);
		for (int a : A) System.out.println(a);
	}
}
```
###### 要素の挿入
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt() - 1;
		int k = sc.nextInt();
		sc.nextLine();
		List<Integer> A = new ArrayList<>(Arrays.stream(sc.nextLine().split(" ")).map(Integer::valueOf).toList());
		sc.close();
		A.add(m, k);
		for (int a : A) System.out.println(a);
	}
}
```
- - -
##### JavaScript
###### 配列末尾への追加
```js
let [[n, m], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
A.push(m);
A.forEach(a => console.log(a));
```
###### 要素の削除
```js
let [[n, m], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
m -= 1;
A.splice(m, 1);
A.forEach(a => console.log(a));
```
###### 要素の挿入
```js
let [[n, m, k], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
m -= 1;
A.splice(m, 0, k);
A.forEach(a => console.log(a));
```
- - -
##### Kotlin
###### 配列末尾への追加
```kotlin
fun main() {
	val (_, m) = readLine()!!.split(' ').map { it.toInt() }
	val A = readLine()!!.split(' ').map { it.toInt() }.toMutableList()
	A.add(m)
	for (a in A) println(a)
}
```
###### 要素の削除
```kotlin
fun main() {
	var (_, m) = readLine()!!.split(' ').map { it.toInt() }
	m -= 1
	val A = readLine()!!.split(' ').map { it.toInt() }.toMutableList()
	A.removeAt(m)
	for (a in A) println(a)
}
```
###### 要素の挿入
```kotlin
fun main() {
	var (_, m, k) = readLine()!!.split(' ').map { it.toInt() }
	m -= 1
	val A = readLine()!!.split(' ').map { it.toInt() }.toMutableList()
	A.add(m, k)
	for (a in A) println(a)
}
```
- - -
##### PHP
###### 配列末尾への追加
```php
<?php
	[$n, $m] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$A[] = $m;
	foreach ($A as $a) echo $a, PHP_EOL;
?>
```
###### 要素の削除
```php
<?php
	[$n, $m] = array_map("intval", explode(' ', fgets(STDIN)));
	$m -= 1;
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	array_splice($A, $m, 1);
	foreach ($A as $a) echo $a, PHP_EOL;
?>
```
###### 要素の挿入
```php
<?php
	[$n, $m, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$m -= 1;
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	array_splice($A, $m, 0, $k);
	foreach ($A as $a) echo $a, PHP_EOL;
?>
```
- - -
##### Perl
###### 配列末尾への追加
```pl
my ($n, $m) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
push @A, $m;
foreach $a (@A) {
	print "$a$/";
}
```
###### 要素の削除
```pl
my ($n, $m) = map { int($_) } split ' ', <STDIN>;
$m -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
splice @A, $m, 1;
foreach $a (@A) {
	print "$a$/";
}
```
###### 要素の挿入
```pl
my ($n, $m, $k) = map { int($_) } split ' ', <STDIN>;
$m -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
splice @A, $m, 0, $k;
foreach $a (@A) {
	print "$a$/";
}
```
- - -
##### Python3
###### 配列末尾への追加
```py
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.append(m)
for a in A:
	print(a)
```
###### 要素の削除
```py
n, m = map(int, input().split())
m -= 1 
A = list(map(int, input().split()))
del A[m]
for a in A:
	print(a)
```
###### 要素の挿入
```py
n, m, k = map(int, input().split())
m -= 1 
A = list(map(int, input().split()))
A.insert(m, k)
for a in A:
	print(a)
```
- - -
##### Ruby
###### 配列末尾への追加
```rb
N, M = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
A << M
A.each do |a|
	p a
end
```
###### 要素の削除
```rb
N, m = gets.split.map(&:to_i)
m -= 1
A = gets.split.map(&:to_i)
A.delete_at(m)
A.each do |a|
	p a
end
```
###### 要素の挿入
```rb
N, m, K = gets.split.map(&:to_i)
m -= 1
A = gets.split.map(&:to_i)
A.insert(m, K)
A.each do |a|
	p a
end
```
- - -
##### Scala
###### 配列末尾への追加
```scala
import scala.io.StdIn._

object Main extends App{
	val Array(_, m) = readLine().split(' ').map { _.toInt }
	var A = readLine().split(' ').map { _.toInt }
	A = A ++ Array(m)
	for (a <- A) println(a)
}
```
```scala
import scala.io.StdIn._
import scala.collection.mutable._

object Main extends App{
	val Array(_, m) = readLine().split(' ').map { _.toInt }
	val A = ArrayBuffer[Int]() ++ readLine().split(' ').map { _.toInt }
	A.append(m)
	for (a <- A) println(a)
}
```
###### 要素の削除
```scala
import scala.io.StdIn._
import scala.collection.mutable._

object Main extends App{
	var Array(_, m) = readLine().split(' ').map { _.toInt }
	m -= 1
	val A = ArrayBuffer[Int]() ++ readLine().split(' ').map { _.toInt }
	A.remove(m)
	for (a <- A) println(a)
}
```
###### 要素の挿入
```scala
import scala.io.StdIn._
import scala.collection.mutable._

object Main extends App{
	var Array(_, m, k) = readLine().split(' ').map { _.toInt }
	m -= 1
	val A = ArrayBuffer[Int]() ++ readLine().split(' ').map { _.toInt }
	A.insert(m, k)
	for (a <- A) println(a)
}
```
- - -
##### Swift
###### 配列末尾への追加
```swift
let nm = readLine()!.split(separator: " ").compactMap { Int($0) }
let m = nm[1]
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.append(m)
for a in A {
	print(a)
}
```
###### 要素の削除
```swift
let nm = readLine()!.split(separator: " ").compactMap { Int($0) }
let m = nm[1] - 1
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.remove(at: m)
for a in A {
	print(a)
}
```
###### 要素の挿入
```swift
let nmk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (m, k) = (nmk[1] - 1, nmk[2])
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
A.insert(k, at: m)
for a in A {
	print(a)
}
```