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ラーニングレベルアップ問題集の条件を満たす要素のみの配列作成をやってみました。


問題


問題のURLにもある通り、filter関数を使える言語では使っていきたいと思います。


C
#include <stdio.h>

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

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

int main() {
	int n, k;
	cin >> n >> k;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	vector<int> B;
	for (int i = 0; i < n; i++) if (A[i] >= k) B.push_back(A[i]);
	for (int b : B) cout << b << endl;
	return 0;
}

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

class Program
{
	public static void Main()
	{
		int[] nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
		int n = nk[0], k = nk[1];
		int[] A = Enumerable.Range(0, n).Select(_ => int.Parse(Console.ReadLine())).ToArray();
		int[] B = A.Where(a => a >= k).ToArray();
		foreach (int b in B) Console.WriteLine(b);
	}
}

Go
package main
import "fmt"

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	var B []int
	for _, a := range(A) {
		if a >= k {
			B = append(B, a)
		}
	}
	for _, b := range(B) {
		fmt.Println(b)
	}
}

Java
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 k = sc.nextInt();
		int[] A = IntStream.range(0, n).map(__ -> sc.nextInt()).toArray();
		sc.close();
		int[] B = Arrays.stream(A).filter(a -> a >= k).toArray();
		for (int b : B) System.out.println(b);
	}
}

JavaScript
const [[n, k], ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? Number(s) : s.split(' ').map(Number));
const B = A.filter(a => a >= k);
B.forEach(b => console.log(b));

Kotlin
fun main() {
	val (n, k) = readLine()!!.split(' ').map { it.toInt() }
	val A = Array(n) { readLine()!!.toInt() }
	val B = A.filter { it >= k }
	for (b in B) println(b)
}

PHP
<?php
	[$n, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = [];
	for ($i = 0; $i < $n; $i++) $A[] = intval(fgets(STDIN));
	$B = array_filter($A, fn($a) => $a >= $k);
	foreach ($B as $b) echo $b, PHP_EOL;
?>

Perl
my ($n, $k) = map { int($_) } split ' ', <STDIN>;
my @A;
for (1..$n) {
	push @A, int(<STDIN>);
}
my @B = grep { $_ >= $k } @A;
foreach $b (@B) {
	print "$b$/";
}

Python3
n, k = map(int, input().split())
A = list(map(lambda _ : int(input()), range(n)))
B = list(filter(lambda a: a >= k, A))
for b in B:
	print(b)
n, k = map(int, input().split())
A = [int(input()) for _ in range(n)]
B = [a for a in A if a >= k]
for b in B:
	print(b)

Ruby
N, K = gets.split.map(&:to_i)
A = N.times.map { gets.to_i }
B = A.select { |a| a >= K}
B.each do |b|
	p b
end

Scala
import scala.io.StdIn._

object Main extends App{
	val Array(n, k) = readLine().split(' ').map { _.toInt }
	val A = (0 until n).map { _ => readInt() }
	val B = A.filter { _ >= k }
	for (b <- B) println(b)
}

Swift
let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k) = (nk[0], nk[1])
let A = (0..<n).map { _ in Int(readLine()!)! }
let B = A.filter { $0 >= k }
for b in B {
	print(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?