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ラーニングレベルアップ問題集の動的配列をやってみました。


問題


C
#include <stdio.h>

int main() {
	int n, Q;
	scanf("%d %d", &n, &Q);
	int A[n + Q];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	while (Q--) {
		int q;
		scanf("%d", &q);
		int x;
		switch (q) {
		case 0: // push_back x
			scanf("%d", &x);
			A[n++] = x;
			break;
		case 1: // pop_back
			--n;
			break;
		case 2: // print
			for (int i = 0; i < n; i++) {
				if (i) putchar(' ');
				printf("%d", A[i]);
			}
			puts("");
			break;
		}
	}
	return 0;
}

C++
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, Q;
	cin >> n >> Q;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	while (Q--) {
		int q;
		cin >> q;
		int x;
		switch (q) {
		case 0: // push_back x
			cin >> x;
			A.push_back(x);
			break;
		case 1: // pop_back
			A.pop_back();
			break;
		case 2: // print
			for (vector<int>::iterator it = A.begin(); it != A.end(); it++) {
				if (it != A.begin()) cout << ' ';
				cout << *it;
			}
			cout << endl;
		}
	}
	return 0;
}

C#
using System;
using System.Collections.Generic;

class Program
{
	public static void Main()
	{
		int Q = int.Parse(Console.ReadLine().Split()[1]);
		List<int> A = new List<int>(Array.ConvertAll(Console.ReadLine().Split(), int.Parse));
		for (; Q > 0; Q--) {
			int[] query = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
			int q = query[0];
			switch (q) {
			case 0: // push_back x
				int x = query[1];
				A.Add(x);
				break;
			case 1: // pop_back
				A.RemoveAt(A.Count - 1);
				break;
			case 2: // print
				System.Console.WriteLine(string.Join(" ", A));
				break;
			}
		}
	}
}

Go
package main
import "fmt"

func main() {
	var n, Q int
	fmt.Scan(&n, &Q)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	for Q > 0 {
		var q int
		fmt.Scan(&q)
		switch q {
		case 0: // push_back x
			var x int
			fmt.Scan(&x)
			A = append(A, x)
		case 1: // pop_back
			A = A[:len(A)-1]
		case 2: // print
			for i, a := range(A) {
				if i > 0 {
					fmt.Print(" ")
				}
				fmt.Print(a)
			}
			fmt.Println()
		}
		Q -= 1
	}
}

Java
import java.util.*;
import java.util.stream.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int Q = sc.nextInt();
		sc.nextLine();
		List<Integer> A = new ArrayList<>(Arrays.asList(Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).boxed().toArray(Integer[]::new)));
		for (; Q > 0; Q--) {
			int q = sc.nextInt();
			switch (q) {
			case 0: // push_back x
				A.add(sc.nextInt());
				break;
			case 1: // pop_back
				A.remove(A.size() - 1);
				break;
			case 2: // print
				System.out.println(String.join(" ", A.stream().map(String::valueOf).toArray(String[]::new)));
				break;
			}
		}
		sc.close();
	}
}

JavaScript
const lines = require("fs").readFileSync("/dev/stdin", "utf8").split('\n');
let [_, Q] = lines.shift().split(' ').map(Number);
const A = lines.shift().split(' ').map(Number);
while (Q--) {
	const query = lines.shift().split(' ').map(Number);
	const q = query[0];
	switch (q) {
	case 0: // push_back x
		const x = query[1];
		A.push(x);
		break;
	case 1: // pop_back
		A.pop();
		break;
	case 2: // print
		console.log(...A);
		break;
	}
}

Kotlin
fun main() {
	val (_, Q) = readln().split(' ').map { it.toInt() }
	val A = readln().split(' ').map { it.toInt() }.toMutableList()
	repeat (Q) {
		val query = readln().split(' ').map { it.toInt() }
		val q = query[0]
		if (q == 0) { // push_back x
			val x = query[1]
			A.add(x)
		} else if (q == 1) { // pop_back
			A.removeLast()
		} else if (q == 2) { // print
			println(A.joinToString(" "))
		}
	}
}

PHP
<?php
	[$n, $Q] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	while ($Q--) {
		$query = array_map("intval", explode(' ', fgets(STDIN)));
		$q = $query[0];
		switch ($q) {
		case 0: // push_back x
			$x = $query[1];
			array_push($A, $x);
			break;
		case 1: // pop_back
			array_pop($A);
			break;
		case 2: // print
			echo implode(' ', $A), PHP_EOL;
			break;
		}
	}
?>

Perl
my ($n, $Q) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
while ($Q--) {
	my @query = map { int($_) } split ' ', <STDIN>;
	my $q = $query[0];
	if ($q == 0) { # push_back x
		my $x = $query[1];
		push @A, $x;
	} elsif ($q == 1) { # pop_back
		pop @A;
	} elsif ($q == 2) { # print
		print join(' ', @A), $/;
	}
}

Python3
n, Q = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(Q):
	query = list(map(int, input().split()))
	q = query[0]
	if q == 0: # push_back x
		x = query[1]
		A.append(x)
	elif q == 1: # pop_back
		A.pop()
	elif q == 2: # print
		print(*A)

Ruby
N, Q = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
Q.times do
	query = gets.split.map(&:to_i)
	q = query[0]
	case q
	when 0 # push_back x
		x = query[1]
		A.push(x)
	when 1
		A.pop
	when 2
		puts A.join(' ')
	end
end

Scala
import scala.io.StdIn._

object Main extends App{
	val Array(n, q) = readLine().split(' ').map { _.toInt }
	val A = readLine().split(' ').map { _.toInt }.toBuffer
	for (_ <- 0 until q) {
		val query = readLine().split(' ').map { _.toInt }
		if (query(0) == 0) { // push_back x
			val x = query(1)
			A += x
		} else if (query(0) == 1) { // pop_back
			A.remove(A.length - 1)
		} else if (query(0) == 2) { // print
			println(A.mkString(" "))
		}
	}
}

Swift
let Q = Int(readLine()!.split(separator: " ")[1])!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
for _ in 0..<Q {
	let query = readLine()!.split(separator: " ").compactMap { Int($0) }
	let q = query[0]
	switch q {
	case 0: // push_back x
		let x = query[1]
		A.append(x)
	case 1: // pop_back
		A.remove(at: A.count - 1)
	case 2: // print
		print(A.map { String($0) }.joined(separator: " "))
	default:
		break
	}
}
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?