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ラーニングレベルアップ問題集の【配列メニュー】をやってみた。(11)

Posted at

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


問題
部分配列

配列の連結


部分配列の1行目で与えられる整数$A$は受け取った直後に$1$引いておくとよいかも($B$は一部の言語を除き引かなくてよい)。


C
部分配列
#include <stdio.h>
#include <string.h>

int main() {
	int a, b, n;
	scanf("%d %d %d", &a, &b, &n);
	a -= 1;
	int m = b - a;
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	int B[m];
	memcpy(B, A + a, sizeof(B));
	for (int i = 0; i < m; i++) printf("%d\n", B[i]);
	return 0;
}
配列の連結
#include <stdio.h>
#include <string.h>

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	int B[m];
	for (int i = 0; i < m; i++) scanf("%d", &B[i]);
	int k = n + m;
	int C[k];
	memcpy(C, A, sizeof(A));
	memcpy(C + n, B, sizeof(B));
	for (int i = 0; i < k; i++) printf("%d\n", C[i]);
	return 0;
}

C++
部分配列
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int a, b, n;
	cin >> a >> b >> n;
	a -= 1;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	vector<int> B(A.begin() + a, A.begin() + b);
	for (int k : B) cout << k << endl;
	return 0;
}
配列の連結
#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];
	vector<int> B(m);
	for (int i = 0; i < m; i++) cin >> B[i];
	A.insert(A.end(), B.begin(), B.end());
	for (int a : A) cout << a << endl;
	return 0;
}

C#
部分配列
using System;

class Program
{
	public static void Main()
	{
		int[] abn = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = abn[0] - 1, b = abn[1];
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] B = new int[b - a];
		Array.Copy(A, a, B, 0, B.Length);
		foreach (int k in B) Console.WriteLine(k);
	}
}
配列の連結
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] B = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] C = A.Concat(B).ToArray();
		foreach (int c in C) Console.WriteLine(c);
	}
}

Go
部分配列
package main
import "fmt"

func main() {
	var a, b, n int
	fmt.Scan(&a, &b, &n)
	a -= 1
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	B := A[a:b]
	for _, k := range(B) {
		fmt.Println(k)
	}
}
配列の連結
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])
	}
	B := make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Scan(&B[i])
	}
	A = append(A, B...)
	for _, a := range(A) {
		fmt.Println(a)
	}
}

Java
部分配列
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt() - 1;
		int b = sc.nextInt();
		int n = sc.nextInt();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int[] B = Arrays.copyOfRange(A, a, b);
		for (int k : B) System.out.println(k);
	}
}
配列の連結
import java.util.*;
import java.util.stream.IntStream;

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();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		int[] B = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		int[] C = IntStream.concat(Arrays.stream(A), Arrays.stream(B)).toArray();
		for (int c : C) System.out.println(c);
		sc.close();
	}
}

JavaScript
部分配列
const [[a, b, n], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
const B = A.slice(a - 1, b);
B.forEach(k => console.log(k));
配列の連結
const [[n, m], A, B] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
const C = A.concat(B);
C.forEach(c => console.log(c));

Kotlin
部分配列
fun main() {
	val (a, b, _) = readLine()!!.split(' ').map { it.toInt() }
	val A = readLine()!!.split(' ').map { it.toInt() }
	val B = A.slice(a-1 until b)
	for (k in B) println(k)
}
配列の連結
fun main() {
	val _ = readLine()!!.split(' ').map { it.toInt() }
	val A = readLine()!!.split(' ').map { it.toInt() }
	val B = readLine()!!.split(' ').map { it.toInt() }
	val C = A + B
	for (c in C) println(c)
}

PHP
部分配列
<?php
	[$a, $b, $n] = array_map("intval", explode(' ', fgets(STDIN)));
	$a -= 1;
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$B = array_slice($A, $a, $b-$a);
	foreach ($B as $k) echo $k, PHP_EOL;
?>
配列の連結
<?php
	[$n, $m] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$B = array_map("intval", explode(' ', fgets(STDIN)));
	$C = array_merge($A, $B);
	foreach ($C as $c) echo $c, PHP_EOL;
?>

Perl
部分配列
my ($a, $b, $n) = map { int($_) } split ' ', <STDIN>;
$a -= 1;
$b -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
my @B = @A[$a..$b];
for $k (@B) {
	print "$k$/";
}
配列の連結
my ($n, $m) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
my @B = map { int($_) } split ' ', <STDIN>;
push @A, @B;
for $a (@A) {
	print "$a$/";
}

Python3
部分配列
a, b, n = map(int, input().split())
a -= 1
A = list(map(int, input().split()))
B = A[a:b]
for k in B:
	print(k)
配列の連結
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A += B
for a in A:
	print(a)

Ruby
部分配列
a, b, n = gets.split.map(&:to_i)
a -= 1
A = gets.split.map(&:to_i)
B = A.slice(a...b)
B.each do |k|
	p k
end
配列の連結
n, m = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = gets.split.map(&:to_i)
A.concat(B)
A.each do |a|
	p a
end

Scala
部分配列
import scala.io.StdIn._

object Main extends App{
	var Array(a, b, _) = readLine().split(' ').map { _.toInt }
	a -= 1
	val A = readLine().split(' ').map { _.toInt }
	val B = A.slice(a, b)
	for (k <- B) println(k)
}
配列の連結
import scala.io.StdIn._

object Main extends App{
	readLine().split(' ').map { _.toInt }
	val A = readLine().split(' ').map { _.toInt }
	val B = readLine().split(' ').map { _.toInt }
	val C = A ++ B
	for (c <- C) println(c)
}

Swift
部分配列
let abn = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (abn[0] - 1, abn[1])
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let B = A[a..<b]
for k in B {
	print(k)
}
配列の連結
let _ = readLine()!.split(separator: " ").compactMap { Int($0) }
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
let B = readLine()!.split(separator: " ").compactMap { Int($0) }
A += B
for a in A {
	print(a)
}
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?