paizaラーニングレベルアップ問題集の「【配列の検索】何個ある?」「指定の要素のカウント」をやってみました。
問題
【配列の検索】何個ある?
指定の要素のカウント
- 1問目は
- 1行目は$N$(検索対象)、$M$(配列の要素数)の順に入力
- 配列は半角区切り入力(ヨコ入力)
- 制約条件は1以上10以下
- $N$は$M$個の整数の中に1個以上含まれることが保証されている
- 2問目は
- 1行目は$N$(配列の要素数)、$K$(検索対象)の順に入力
- 配列は改行区切り入力(タテ入力)
- 制約条件は1以上100以下
- 入力例1のような、答えが0の可能性がある
です。
C
【配列の検索】何個ある?
#include <stdio.h>
int main() {
int n, m;
scanf("%d %d", &n, &m);
int A[m];
for (int i = 0; i < m; i++) scanf("%d", &A[i]);
int cnt = 0;
for (int i = 0; i < m; i++) if (A[i] == n) cnt += 1;
printf("%d\n", cnt);
return 0;
}
指定の要素のカウント
#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 cnt = 0;
for (int i = 0; i < n; i++) if (A[i] == k) cnt += 1;
printf("%d\n", cnt);
return 0;
}
C++
【配列の検索】何個ある?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> A(m);
for (int i = 0; i < m; i++) cin >> A[i];
cout << count(A.begin(), A.end(), n) << endl;
return 0;
}
指定の要素のカウント
#include <iostream>
#include <vector>
#include <algorithm>
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];
cout << count(A.begin(), A.end(), k) << endl;
return 0;
}
C#
【配列の検索】何個ある?
using System;
using System.Linq;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine().Split()[0]);
Console.WriteLine(Console.ReadLine().Split().Select(_ => int.Parse(_)).Where(_ => _ == n).Count());
}
}
指定の要素のカウント
using System;
using System.Linq;
class Program
{
public static void Main()
{
int[] nk = Console.ReadLine().Split().Select(_ => int.Parse(_)).ToArray();
int n = nk[0], k = nk[1];
Console.WriteLine(Enumerable.Range(0, n).Select(_ => int.Parse(Console.ReadLine())).Where(_ => _ == k).Count());
}
}
Go
【配列の検索】何個ある?
package main
import "fmt"
func main() {
var n, m int
fmt.Scan(&n, &m)
A := make([]int, m)
for i := 0; i < m; i++ {
fmt.Scan(&A[i])
}
cnt := 0
for i := 0; i < m; i++ {
if (A[i] == n) {
cnt += 1
}
}
fmt.Println(cnt)
}
指定の要素のカウント
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])
}
cnt := 0
for i := 0; i < n; i++ {
if (A[i] == k) {
cnt += 1
}
}
fmt.Println(cnt)
}
Java
【配列の検索】何個ある?
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine().split(" ")[0]);
System.out.println(Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).filter(a -> a == n).count());
sc.close();
}
}
指定の要素のカウント
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();
System.out.println(IntStream.range(0, n).map(i -> sc.nextInt()).filter(a -> a == k).count());
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.filter(a => a == n).length);
指定の要素のカウント
const [[n, k], ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? Number(s) : s.split(' ').map(Number));
console.log(A.filter(a => a == k).length);
Kotlin
【配列の検索】何個ある?
fun main() {
val (n, _) = readLine()!!.split(' ').map { it.toInt() }
val A = readLine()!!.split(' ').map { it.toInt() }
println(A.count { it == n })
}
指定の要素のカウント
fun main() {
val (n, k) = readLine()!!.split(' ').map { it.toInt() }
val A = Array(n) { readLine()!!.toInt() }
println(A.count { it == k })
}
PHP
【配列の検索】何個ある?
<?php
[$n, $m] = array_map("intval", explode(' ', fgets(STDIN)));
$A = array_map("intval", explode(' ', fgets(STDIN)));
echo count(array_filter($A, fn($a) => $a == $n)), PHP_EOL;
?>
指定の要素のカウント
<?php
[$n, $k] = array_map("intval", explode(' ', fgets(STDIN)));
$A = [];
for ($i = 0; $i < $n; $i++) $A[] = intval(fgets(STDIN));
echo array_count_values($A)[$k], PHP_EOL;
?>
Perl
【配列の検索】何個ある?
my ($n, $m) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
my $cnt;
for (@A) {
$cnt++ if $_ == $n;
}
print "$cnt$/";
指定の要素のカウント
my ($n, $k) = map { int($_) } split ' ', <STDIN>;
my @A;
for (1..$n) {
push @A, int(<STDIN>);
}
my $cnt;
for (@A) {
$cnt++ if $_ == $k;
}
print "$cnt$/";
- - -
##### Python3
###### 【配列の検索】何個ある?
```py
n, _ = map(int, input().split())
print(list(map(int, input().split())).count(n))
指定の要素のカウント
n, k = map(int, input().split())
print([int(input()) for _ in range(n)].count(k))
Ruby
【配列の検索】何個ある?
n, _ = gets.split.map(&:to_i)
print(gets.split.map(&:to_i).filter { |a| a == n }.count)
指定の要素のカウント
n, k = gets.split.map(&:to_i)
print(n.times.map { gets.to_i }.filter { |a| a == k }.count)
Scala
【配列の検索】何個ある?
import scala.io.StdIn._
object Main extends App{
val Array(n, m) = readLine().split(' ').map { _.toInt }
println(readLine().split(' ').map { _.toInt }.count { _ == n })
}
指定の要素のカウント
import scala.io.StdIn._
object Main extends App{
val Array(n, k) = readLine().split(' ').map { _.toInt }
println((0 until n).map { _ => readInt() }.count { _ == k })
}
Swift
【配列の検索】何個ある?
let nm = readLine()!.split(separator: " ").compactMap { Int($0) }
let n = nm[0]
print(readLine()!.split(separator: " ").compactMap { Int($0) }.filter { $0 == n }.count)
指定の要素のカウント
let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k) = (nk[0], nk[1])
var A: [Int] = []
for _ in 0..<n {
A.append(Int(readLine()!)!)
}
print(A.filter { $0 == k }.count)