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, m;
	scanf("%d %d", &n, &m);
	m -= 1;
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	printf("%d\n", A[m]);
	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 q;
	while (q--) {
		int b;
		scanf("%d", &b);
		b -= 1;
		printf("%d\n", A[b]);
	}
	return 0;
}

C++
ランダムアクセス
#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];
	cout << A[m] << endl;
	return 0;
}
複数回のランダムアクセス
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	int q;
	cin >> q;
	while (q--) {
		int b;
		cin >> b;
		b -= 1;
		cout << A[b] << endl;
	}
	return 0;
}

C#
ランダムアクセス
using System;

class Program
{
	public static void Main()
	{
		int[] nm = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int m = nm[1] - 1;
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Console.WriteLine(A[m]);
	}
}
複数回のランダムアクセス
using System;

class Program
{
	public static void Main()
	{
		Console.ReadLine();
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Console.ReadLine();
		int[] B = Array.ConvertAll(Console.ReadLine().Split(), b => int.Parse(b) - 1);
		foreach (int b in B) Console.WriteLine(A[b]);
	}
}

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])
	}
	fmt.Println(A[m])
}
複数回のランダムアクセス
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])
	}
	var Q int
	fmt.Scan(&Q)
	for q := 0; q < Q; q++ {
		var b int
		fmt.Scan(&b)
		b -= 1
		fmt.Println(A[b])
	}
}

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();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		System.out.println(A[m]);
	}
}
複数回のランダムアクセス
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();
		for (int q = sc.nextInt(); q > 0; q--) {
			int b = sc.nextInt() - 1;
			System.out.println(A[b]);
		}
		sc.close();
	}
}

JavaScript
ランダムアクセス
const [[n, m], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
console.log(A[m-1]);
複数回のランダムアクセス
const [[n], A, [q], B] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
B.forEach(b => console.log(A[b-1]));

Kotlin
ランダムアクセス
fun main() {
	val (_, m) = readln().split(' ').map { it.toInt() }
	val A = readln().split(' ').map { it.toInt() }
	println(A[m-1])
}
複数回のランダムアクセス
fun main() {
	readln()
	val A = readln().split(' ').map { it.toInt() }
	readln()
	val B = readln().split(' ').map { it.toInt() - 1 }
	for (b in B) println(A[b])
}

PHP
ランダムアクセス
<?php
	[$n, $m] = array_map("intval", explode(' ', fgets(STDIN)));
	$m -= 1;
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	echo $A[$m], PHP_EOL;
?>
複数回のランダムアクセス
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$q = intval(fgets(STDIN));
	$B = array_map(fn($b) => intval($b) - 1, explode(' ', fgets(STDIN)));
	foreach ($B as $b) echo $A[$b], PHP_EOL;
?>

Perl
ランダムアクセス
my ($n, $m) = map { int($_) } split ' ', <STDIN>;
$m -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
print "$A[$m]$/";
複数回のランダムアクセス
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $q = int(<STDIN>);
my @B = map { int($_) - 1 } split ' ', <STDIN>;
for $b (@B) {
	print "$A[$b]$/";
}

Python3
ランダムアクセス
n, m = map(int, input().split())
m -= 1
A = list(map(int, input().split()))
print(A[m])
複数回のランダムアクセス
n = int(input())
A = list(map(int, input().split()))
q = int(input())
B = list(map(lambda x : int(x)-1, input().split()))
for b in B:
	print(A[b])

Ruby
ランダムアクセス
N, m = gets.split.map(&:to_i)
m -= 1
A = gets.split.map(&:to_i)
p A[m]
複数回のランダムアクセス
N = gets.to_i
A = gets.split.map(&:to_i)
Q = gets.to_i
B = gets.split.map { |x| x.to_i - 1 }
B.each do |b|
	p A[b]
end

Scala
ランダムアクセス
import scala.io.StdIn._

object Main extends App{
	val Array(n, m) = readLine().split(' ').map { _.toInt }
	val A = readLine().split(' ').map { _.toInt }
	println(A(m-1))
}
複数回のランダムアクセス
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	val q = readInt()
	val B = readLine().split(' ').map { _.toInt - 1 }
	for (b <- B) println(A(b))
}

Swift
ランダムアクセス
let nm = readLine()!.split(separator: " ").compactMap { Int($0) }
let m = nm[1] - 1
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
print(A[m])
複数回のランダムアクセス
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let q = Int(readLine()!)!
let B = readLine()!.split(separator: " ").map { Int($0)! - 1 }
for b in B {
	print(A[b])
}
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?