paizaラーニングレベルアップ問題集の「ある数以上以下の要素の列挙」をやってみた。

paizaラーニングレベルアップ問題集の「ある数以上以下の要素の列挙」をやってみました。
- - -
##### 問題
###### STEP: 1
https://paiza.jp/works/mondai/array_primer/array_primer__array_ave_step1
###### STEP: 2
https://paiza.jp/works/mondai/array_primer/array_primer__array_ave_step2
###### STEP: 3
https://paiza.jp/works/mondai/array_primer/array_primer__array_ave_step3
- - -
3問目は、
```py
n = int(input())
A = list(map(int, input().split()))
avg = sum(A) / n
for a in A: if a >= avg: print(a)
```
と、「平均」と比較する方法もありますが、
```math
a_i\ge\frac{S}{n}\iff a_i\times n\ge S
```
と式変形して、整数同士で比較するようにします。
- - -
##### C
###### STEP: 1
```c
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	for (int i = 0; i < n; i++) if (A[i] >= 5) printf("%d\n", A[i]);
	return 0;
}
```
###### STEP: 2
```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]);
	for (int i = 0; i < n; i++) if (A[i] >= k) printf("%d\n", A[i]);
	return 0;
}
```
###### STEP: 3
```c
#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 s = 0;
	for (int i = 0; i < n; i++) s += A[i];
	for (int i = 0; i < n; i++) if (A[i] * n >= s) printf("%d\n", A[i]);
	return 0;
}
```
- - -
##### C++
###### STEP: 1
```cpp
#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];
	for (int i = 0; i < n; i++) if (A[i] >= 5) cout << A[i] << endl;
	return 0;
}
```
###### STEP: 2
```cpp
#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];
	for (int i = 0; i < n; i++) if (A[i] >= k) cout << A[i] << endl;
	return 0;
}
```
###### STEP: 3
```cpp
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	int s = accumulate(A.begin(), A.end(), 0);
	for (int i = 0; i < n; i++) if (A[i] * n >= s) cout << A[i] << endl;
	return 0;
}
```
- - -
##### C#
###### STEP: 1
```cs
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		Console.ReadLine();
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] B = A.Where(_ => _ >= 5).ToArray();
		foreach (int b in B) Console.WriteLine(b);
	}
}
```
###### STEP: 2
```cs
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		int[] nk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int k = nk[1];
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int[] B = A.Where(_ => _ >= k).ToArray();
		foreach (int b in B) Console.WriteLine(b);
	}
}
```
###### STEP: 3
```cs
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int s = A.Sum();
		int[] B = A.Where(_ => _ * n >= s).ToArray();
		foreach (int b in B) Console.WriteLine(b);
	}
}
```
- - -
##### Go
###### STEP: 1
```go
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])
	}
	for i := 0; i < n; i++ {
		if (A[i] >= 5) {
			fmt.Println(A[i])
		}
	}
}
```
###### STEP: 2
```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])
	}
	for i := 0; i < n; i++ {
		if (A[i] >= k) {
			fmt.Println(A[i])
		}
	}
}
```
###### STEP: 3
```go
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])
	}
	s := 0
	for i := 0; i < n; i++ {
		s += A[i]
	}
	for i := 0; i < n; i++ {
		if (A[i] * n >= s) {
			fmt.Println(A[i])
		}
	}
}
```
- - -
##### Java
###### STEP: 1
```java
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();
		sc.close();
		int[] B = Arrays.stream(A).filter(a -> a >= 5).toArray();
		for (int b : B) System.out.println(b);
	}
}
```
###### STEP: 2
```java
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int[] B = Arrays.stream(A).filter(a -> a >= k).toArray();
		for (int b : B) System.out.println(b);
	}
}
```
###### STEP: 3
```java
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();
		sc.close();
		int s = Arrays.stream(A).sum();
		int[] B = Arrays.stream(A).filter(a -> a * n >= s).toArray();
		for (int b : B) System.out.println(b);
	}
}
```
- - -
##### JavaScript
###### STEP: 1
```js
const [n, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
A.filter(_ => _ >= 5).forEach(_ => console.log(_));
```
###### STEP: 2
```js
const [[n, k], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
A.filter(_ => _ >= k).forEach(_ => console.log(_));
```
###### STEP: 3
```js
const [n, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((_, i) => i ? _.split(' ').map(Number) : Number(_));
const s = A.reduce((s, a) => s + a, 0);
A.filter(_ => _ * n >= s).forEach(_ => console.log(_));
```
- - -
##### Kotlin
###### STEP: 1
```kotlin
fun main() {
	readLine()
	val A = readLine()!!.split(' ').map { it.toInt() }
	val B = A.filter { it >= 5 }
	for (b in B) println(b)
}
```
###### STEP: 2
```kotlin
fun main() {
	val (_, k) = readLine()!!.split(' ').map { it.toInt() }
	val A = readLine()!!.split(' ').map { it.toInt() }
	val B = A.filter { it >= k }
	for (b in B) println(b)
}
```
###### STEP: 3
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	val s = A.sum()
	val B = A.filter { it * n >= s }
	for (b in B) println(b)
}
```
- - -
##### PHP
###### STEP: 1
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(" ", fgets(STDIN)));
	$B = array_filter($A, fn($a) => $a >= 5);
	foreach ($B as $b) echo $b, PHP_EOL;
?>
```
###### STEP: 2
```php
<?php
	[$n, $k] = array_map("intval", explode(" ", fgets(STDIN)));
	$A = array_map("intval", explode(" ", fgets(STDIN)));
	$B = array_filter($A, fn($a) => $a >= $k);
	foreach ($B as $b) echo $b, PHP_EOL;
?>
```
###### STEP: 3
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(" ", fgets(STDIN)));
	$s = array_sum($A);
	$B = array_filter($A, fn($a) => $a * $n >= $s);
	foreach ($B as $b) echo $b, PHP_EOL;
?>
```
- - -
##### Perl
###### STEP: 1
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my @B = grep { $_ >= 5 } @A;
foreach $b (@B) {
	print "$b$/";
}
```
###### STEP: 2
```pl
my ($n, $k) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
my @B = grep { $_ >= $k } @A;
foreach $b (@B) {
	print "$b$/";
}
```
###### STEP: 3
```pl
use List::Util qw(sum);

my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $s = List::Util::sum @A;
my @B = grep { $_ * $n >= $s } @A;
foreach $b (@B) {
	print "$b$/";
}
```
- - -
##### Python3
###### STEP: 1
```py
n = int(input())
A = list(map(int, input().split()))
B = list(filter(lambda a: a >= 5, A))
for b in B:
	print(b)
```
###### STEP: 2
```py
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(filter(lambda a: a >= k, A))
for b in B:
	print(b)
```
###### STEP: 3
```py
n = int(input())
A = list(map(int, input().split()))
s = sum(A)
B = list(filter(lambda a: a * n >= s, A))
for b in B:
	print(b)
```
- - -
##### Ruby
###### STEP: 1
```rb
n = gets.to_i
A = gets.split.map(&:to_i)
B = A.filter { |a| a >= 5 }
B.each do |b|
	p b
end
```
###### STEP: 2
```rb
n, k = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
B = A.filter { |a| a >= k }
B.each do |b|
	p b
end
```
###### STEP: 3
```rb
n = gets.to_i
A = gets.split.map(&:to_i)
s = A.sum
B = A.filter { |a| a * n >= s }
B.each do |b|
	p b
end
```
- - -
##### Scala
###### STEP: 1
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	val B = A.filter { _ >= 5 }
	for (b <- B) println(b)
}
```
###### STEP: 2
```scala
import scala.io.StdIn._

object Main extends App{
	val Array(n, k) = readLine().split(' ').map { _.toInt }
	val A = readLine().split(' ').map { _.toInt }
	val B = A.filter { _ >= k }
	for (b <- B) println(b)
}
```
###### STEP: 3
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	val s = A.sum
	val B = A.filter { _ * n >= s }
	for (b <- B) println(b)
}
```
- - -
##### Swift
###### STEP: 1
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let B = A.filter { $0 >= 5 }
for b in B {
	print(b)
}
```
###### STEP: 2
```swift
let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let k = nk[1]
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let B = A.filter { $0 >= k }
for b in B {
	print(b)
}
```
###### STEP: 3
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let s = A.reduce(0, +)
let B = A.filter { $0 * n >= s }
for b in B {
	print(b)
}
```