paizaラーニングレベルアップ問題集の【ループメニュー】をやってみた。(4)

paizaラーニングレベルアップ問題集の【ループメニュー】をやってみました。
- - -
##### 問題
###### 数列の最大値
https://paiza.jp/works/mondai/loop_problems/loop_problems__seq_max
###### 数列の最小値
https://paiza.jp/works/mondai/loop_problems/loop_problems__seq_min
###### 数列の何番目にある?
https://paiza.jp/works/mondai/loop_problems/loop_problems__seq_i-th
###### 数列の中に何個ある?
https://paiza.jp/works/mondai/loop_problems/loop_problems__seq_count
- - -
便利な組み込み関数もありますが、今回は封印致します。

:::note warn
1問目と2問目の違い
2問目は$0$で初期化しないように！
:::
$a_1$の値を初期値としておけば、最小値が常に$0$になってしまうという事故を防げます。
- - -
##### C
###### 数列の最大値
```c
#include <stdio.h>

int main() {
	int n = 0;
	scanf("%d", &n);
	int max;
	scanf("%d", &max);
	for (int i = 1; i < n; i++) {
		int a;
		scanf("%d", &a);
		if (a > max) max = a;
	}
	printf("%d\n", max);
	return 0;
}
```
###### 数列の最小値
```c
#include <stdio.h>

int main() {
	int n = 0;
	scanf("%d", &n);
	int min;
	scanf("%d", &min);
	for (int i = 1; i < n; i++) {
		int a;
		scanf("%d", &a);
		if (a < min) min = a;
	}
	printf("%d\n", min);
	return 0;
}
```
###### 数列の何番目にある?
```c
#include <stdio.h>

int main() {
	int n = 0;
	scanf("%d", &n);
	int idx = -1;
	for (int i = 0; i < n; i++) {
		int a;
		scanf("%d", &a);
		if (a == 1) idx = i;
	}
	printf("%d\n", idx + 1);
	return 0;
}
```
###### 数列の中に何個ある?
```c
#include <stdio.h>

int main() {
	int n = 0;
	scanf("%d", &n);
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		int a;
		scanf("%d", &a);
		cnt += a == 1;
	}
	printf("%d\n", cnt);
	return 0;
}
```
- - -
##### C++
###### 数列の最大値
```cpp
#include <iostream>
//using namespace std;

int main() {
	int n;
	std::cin >> n;
	int max;
	std::cin >> max;
	for (int i = 1; i < n; i++) {
		int a;
		std::cin >> a;
		if (a > max) max = a;
	}
	std::cout << max << std::endl;
	return 0;
}
```
###### 数列の最小値
```cpp
#include <iostream>
//using namespace std;

int main() {
	int n;
	std::cin >> n;
	int min;
	std::cin >> min;
	for (int i = 1; i < n; i++) {
		int a;
		std::cin >> a;
		if (a < min) min = a;
	}
	std::cout << min << std::endl;
	return 0;
}
```
###### 数列の何番目にある?
```cpp
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int idx = -1;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		if (a == 1) idx = i;
	}
	cout << idx + 1 << endl;
	return 0;
}
```
###### 数列の中に何個ある?
```cpp
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int cnt = 0;
	for (int i = 0; i < n; i++) {
		int a;
		cin >> a;
		cnt += a == 1;
	}
	cout << cnt << endl;
	return 0;
}
```
- - -
##### C#
###### 数列の最大値
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int max = A[0];
		for (int i = 1; i < n; i++) {
			if (A[i] > max) max = A[i]; 
		}
		Console.WriteLine(max);
	}
}
```
###### 数列の最小値
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int min = A[0];
		for (int i = 1; i < n; i++) {
			if (A[i] < min) min = A[i]; 
		}
		Console.WriteLine(min);
	}
}
```
###### 数列の何番目にある?
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int idx = -1;
		for (int i = 0; i < n; i++) {
			if (A[i] == 1) idx = i; 
		}
		Console.WriteLine(idx + 1);
	}
}
```
###### 数列の中に何個ある?
```cs
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			if (A[i] == 1) cnt++; 
		}
		Console.WriteLine(cnt);
	}
}
```
- - -
##### Go
###### 数列の最大値
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var max int
	fmt.Scan(&max)
	for i := 1; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if (a > max) {
			max = a
		}
	}
	fmt.Println(max)
}
```
###### 数列の最小値
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var min int
	fmt.Scan(&min)
	for i := 1; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if (a < min) {
			min = a
		}
	}
	fmt.Println(min)
}
```
###### 数列の何番目にある?
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	idx := -1
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if (a == 1) {
			idx = i
		}
	}
	fmt.Println(idx + 1)
}
```
###### 数列の中に何個ある?
```go
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	cnt := 0
	for i := 0; i < n; i++ {
		var a int
		fmt.Scan(&a)
		if (a == 1) {
			cnt++
		}
	}
	fmt.Println(cnt)
}
```
- - -
##### Java
###### 数列の最大値
```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());
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int max = A[0];
		for (int i = 1; i < n; i++) if (A[i] > max) max = A[i];
		System.out.println(max);
	}
}
```
###### 数列の最小値
```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());
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int min = A[0];
		for (int i = 1; i < n; i++) if (A[i] < min) min = A[i];
		System.out.println(min);
	}
}
```
###### 数列の何番目にある?
```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());
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int idx = -1;
		for (int i = 0; i < n; i++) if (A[i] == 1) idx = i;
		System.out.println(idx + 1);
	}
}
```
###### 数列の中に何個ある?
```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());
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int cnt = 0;
		for (int i = 0; i < n; i++) if (A[i] == 1) cnt++;
		System.out.println(cnt);
	}
}
```
- - -
##### JavaScript
###### 数列の最大値
```js
const [N, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
let max = A[0];
for (var i = 1; i < N; i++) if (A[i] > max) max = A[i];
console.log(max);
```
###### 数列の最小値
```js
const [N, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
let min = A[0];
for (var i = 1; i < N; i++) if (A[i] < min) min = A[i];
console.log(min);
```
###### 数列の何番目にある?
```js
const [N, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
let idx = -1;
for (var i = 0; i < N; i++) if (A[i] == 1) idx = i;
console.log(idx + 1);
```
###### 数列の中に何個ある?
```js
const [N, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
let cnt = 0;
for (var i = 0; i < N; i++) cnt += A[i] == 1;
console.log(cnt);
```
- - -
##### Kotlin
###### 数列の最大値
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	var max = A[0]
	for (i in 1 until n) if (A[i] > max) max = A[i]
	println(max)
}
```
###### 数列の最小値
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	var min = A[0]
	for (i in 1 until n) if (A[i] < min) min = A[i]
	println(min)
}
```
###### 数列の何番目にある?
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	var idx = -1
	for (i in 0 until n) if (A[i] == 1) idx = i
	println(idx + 1)
}
```
###### 数列の中に何個ある?
```kotlin
fun main() {
	val n = readLine()!!.toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	var cnt = 0
	for (i in 0 until n) if (A[i] == 1) cnt++
	println(cnt)
}
```
- - -
##### PHP
###### 数列の最大値
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$max = $A[0];
	for ($i = 1; $i < $n; $i++) if ($A[$i] > $max) $max = $A[$i];
	echo $max, PHP_EOL;
?>
```
###### 数列の最小値
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$min = $A[0];
	for ($i = 1; $i < $n; $i++) if ($A[$i] < $min) $min = $A[$i];
	echo $min, PHP_EOL;
?>
```
###### 数列の何番目にある?
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$idx = -1;
	for ($i = 0; $i < $n; $i++) if ($A[$i] == 1) $idx = $i;
	echo $idx + 1, PHP_EOL;
?>
```
###### 数列の中に何個ある?
```php
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	$cnt = 0;
	for ($i = 0; $i < $n; $i++) $cnt += $A[$i] == 1;
	echo $cnt, PHP_EOL;
?>
```
- - -
##### Perl
###### 数列の最大値
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $max = $A[0];
for (my $i = 1; $i < $n; $i++) {
	if ($A[$i] > $max) {
		$max = $A[$i];
	}
}
print "$max$/";
```
###### 数列の最小値
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $min = $A[0];
for (my $i = 1; $i < $n; $i++) {
	if ($A[$i] > $min) {
		$min = $A[$i];
	}
}
print "$min$/";
```
###### 数列の何番目にある?
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $idx = -1;
for (my $i = 0; $i < $n; $i++) {
	if ($A[$i] == 1) {
		$idx = $i;
	}
}
print $idx + 1, $/;
```
###### 数列の中に何個ある?
```pl
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $cnt = 0;
for (my $i = 0; $i < $n; $i++) {
	$cnt += $A[$i] == 1;
}
print "$cnt$/";
```
- - -
##### Python3
###### 数列の最大値
```py
n = int(input())
A = list(map(int, input().split()))
max = A[0] #組み込み関数maxを封印
for i in range(1, n):
	if A[i] > max:
		max = A[i]
print(max)
```
###### 数列の最小値
```py
n = int(input())
A = list(map(int, input().split()))
min = A[0] #組み込み関数minを封印
for i in range(1, n):
	if A[i] < min:
		min = A[i]
print(min)
```
###### 数列の何番目にある?
```py
n = int(input())
A = list(map(int, input().split()))
idx = -1
for i in range(n):
	if A[i] == 1:
		idx = i
print(idx + 1)
```
###### 数列の中に何個ある?
```py
n = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(n):
	cnt += A[i] == 1
print(cnt)
```
- - -
##### Ruby
###### 数列の最大値
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
max = A[0]
(1...N).each do |i|
	if A[i] > max
		max = A[i]
	end
end
p max
```
###### 数列の最小値
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
min = A[0]
(1...N).each do |i|
	if A[i] < min
		min = A[i]
	end
end
p min
```
###### 数列の何番目にある?
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
idx = -1
N.times do |i|
	if A[i] == 1
		idx = i
	end
end
p idx + 1
```
###### 数列の中に何個ある?
```rb
N = gets.to_i
A = gets.split.map(&:to_i)
cnt = 0
N.times do |i|
	if A[i] == 1
		cnt += 1
	end
end
p cnt
```
- - -
##### Scala
###### 数列の最大値
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	var max = A(0)
	for (i <- 1 until n) if (A(i) > max) max = A(i)
	println(max)
}
```
###### 数列の最小値
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	var min = A(0)
	for (i <- 1 until n) if (A(i) < min) min = A(i)
	println(min)
}
```
###### 数列の何番目にある?
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	var idx = -1
	for (i <- 0 until n) if (A(i) == 1) idx = i
	println(idx + 1)
}
```
###### 数列の中に何個ある?
```scala
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	var cnt = 0
	for (i <- 0 until n) if (A(i) == 1) cnt += 1
	println(cnt)
}
```
- - -
##### Swift
###### 数列の最大値
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var max = A[0] //max関数を封印
for i in 1..<n {
	if A[i] > max {
		max = A[i]
	}
}
print(max)
```
###### 数列の最小値
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var min = A[0] //min関数を封印
for i in 1..<n {
	if A[i] > min {
		min = A[i]
	}
}
print(min)
```
###### 数列の何番目にある?
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var idx = -1
for i in 0..<n {
	if A[i] == 1 {
		idx = i
	}
}
print(idx + 1)
```
###### 数列の中に何個ある?
```swift
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var cnt = 0
for i in 0..<n {
	if A[i] == 1 {
		cnt += 1
	}
}
print(cnt)
```