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ラーニングレベルアップ問題集の「何番目にある?」をやってみました。


問題
STEP: 1

STEP: 2

STEP: 3


プログラム内部では配列の添字は0始まりですが、出力は「左端を1番目とします。」とあります。


C
STEP: 1
#include <stdio.h>

const int A[] = { 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 };
const int N = 8;

int main() {
	size_t m = sizeof(A) / sizeof(A[0]);
	for (size_t i = 0; i < m; i++) {
		if (A[i] == N) {
			printf("%zu\n", i + 1);
			break;
		}
	}
	return 0;
}
STEP: 2
#include <stdio.h>

const int A[] = { 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 };

int main() {
	int n;
	scanf("%d", &n);
	size_t m = sizeof(A) / sizeof(A[0]);
	for (size_t i = 0; i < m; i++) {
		if (A[i] == n) {
			printf("%zu\n", i + 1);
			break;
		}
	}
	return 0;
}
STEP: 3
#include <stdio.h>

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

C++
STEP: 1
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const vector<int> A = { 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 };
const int N = 8;

int main() {
	cout << find(A.begin(), A.end(), N) - A.begin() + 1 << endl;
	return 0;
}
STEP: 2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const vector<int> A = { 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 };

int main() {
	int N;
	cin >> N;
	cout << find(A.begin(), A.end(), N) - A.begin() + 1 << endl;
	return 0;
}
STEP: 3
#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 << find(A.begin(), A.end(), N) - A.begin() + 1 << endl;
	return 0;
}

C#
STEP: 1
using System;

class Program
{
	static readonly int[] A = { 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 };
	const int N = 8;
	
	public static void Main()
	{
		Console.WriteLine(Array.IndexOf(A, N) + 1);
	}
}
STEP: 2
using System;

class Program
{
	static readonly int[] A = { 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 };
	
	public static void Main()
	{
		int N = int.Parse(Console.ReadLine());
		Console.WriteLine(Array.IndexOf(A, N) + 1);
	}
}
STEP: 3
using System;

class Program
{
	public static void Main()
	{
		int N = int.Parse(Console.ReadLine().Split()[0]);
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Console.WriteLine(Array.IndexOf(A, N) + 1);
	}
}

Go
STEP: 1
package main
import "fmt"

var A = []int{ 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 }
const N = 8

func main() {
	for i, a := range(A) {
		if a == N {
			fmt.Println(i + 1)
			break
		}
	}
}
STEP: 2
package main
import "fmt"

var A = []int{ 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 }

func main() {
	var N int
	fmt.Scan(&N)
	for i, a := range(A) {
		if a == N {
			fmt.Println(i + 1)
			break
		}
	}
}
STEP: 3
package main
import "fmt"

var A = []int{ 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 }

func main() {
	var N, M int
	fmt.Scan(&N, &M)
	A := make([]int, M)
	for i := 0; i < M; i++ {
		fmt.Scan(&A[i])
	}
	for i, a := range(A) {
		if a == N {
			fmt.Println(i + 1)
			break
		}
	}
}

Java
STEP: 1
import java.util.*;

public class Main {

	static final Integer[] A = { 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 };
	static final int N = 8;
	
	public static void main(String[] args) {
		System.out.println(Arrays.asList(A).indexOf(N) + 1);
	}
}
STEP: 2
import java.util.*;

public class Main {

	static final Integer[] A = { 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 };
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		sc.close();
		System.out.println(Arrays.asList(A).indexOf(N) + 1);
	}
}
STEP: 3
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();
		Integer[] A = new Integer[m];
		for (int i = 0; i < m; i++) A[i] = sc.nextInt();
		sc.close();
		System.out.println(Arrays.asList(A).indexOf(n) + 1);
	}
}

JavaScript
STEP: 1
const A = [ 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 ];
const N = 8;
console.log(A.indexOf(N) + 1);
STEP: 2
const A = [ 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 ];
const N = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log(A.indexOf(N) + 1);
STEP: 3
const [[N, M], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
console.log(A.indexOf(N) + 1);

Kotlin
STEP: 1
fun main() {
	val A = arrayOf(1, 10, 2, 9, 3, 8, 4, 7, 5, 6)
	val N = 8
	println(A.indexOf(N) + 1)
}
STEP: 2
fun main() {
	val A = arrayOf(1, 5, 9, 7, 3, 2, 4, 8, 6, 10)
	val N = readLine()!!.toInt()
	println(A.indexOf(N) + 1)
}
STEP: 3
fun main() {
	val N = readLine()!!.split(' ')[0].toInt()
	val A = readLine()!!.split(' ').map { it.toInt() }
	println(A.indexOf(N) + 1)
}

PHP
STEP: 1
<?php
	$A = [ 1, 10, 2, 9, 3, 8, 4, 7, 5, 6 ];
	$N = 8;
	echo array_search($N, $A) + 1, PHP_EOL;
?>
STEP: 2
<?php
	$A = [ 1, 5, 9, 7, 3, 2, 4, 8, 6, 10 ];
	$N = intval(fgets(STDIN));
	echo array_search($N, $A) + 1, PHP_EOL;
?>
STEP: 3
<?php
	$N = intval(explode(' ', fgets(STDIN))[0]);
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	echo array_search($N, $A) + 1, PHP_EOL;
?>

Perl
STEP: 1
my @A = (1, 10, 2, 9, 3, 8, 4, 7, 5, 6);
my $N = 8;
my $M = scalar(@A);
for (my $i = 0; $i < $M; $i++) {
	if ($A[$i] == $N) {
		print $i + 1, $/;
		last;
	}
}
STEP: 2
my @A = (1, 5, 9, 7, 3, 2, 4, 8, 6, 10);
my $N = int(<STDIN>);
my $M = scalar(@A);
for (my $i = 0; $i < $M; $i++) {
	if ($A[$i] == $N) {
		print $i + 1, $/;
		last;
	}
}
STEP: 3
my ($N, $M) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
for (my $i = 0; $i < $M; $i++) {
	if ($A[$i] == $N) {
		print $i + 1, $/;
		last;
	}
}

Python3
STEP: 1
A = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]
N = 8
print(A.index(N) + 1)
STEP: 2
A = [1, 5, 9, 7, 3, 2, 4, 8, 6, 10]
N = int(input())
print(A.index(N) + 1)
STEP: 3
N = int(input().split()[0])
A = list(map(int, input().split()))
print(A.index(N) + 1)

Ruby
STEP: 1
A = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]
N = 8
p A.index(N) + 1
STEP: 2
A = [1, 5, 9, 7, 3, 2, 4, 8, 6, 10]
N = gets.to_i
p A.index(N) + 1
STEP: 3
N, _ = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
p A.index(N) + 1

Scala
STEP: 1
object Main extends App{
	val A = Array(1, 10, 2, 9, 3, 8, 4, 7, 5, 6)
	val N = 8
	println(A.indexOf(N) + 1)
}
STEP: 2
import scala.io.StdIn._

object Main extends App{
	val A = Array(1, 5, 9, 7, 3, 2, 4, 8, 6, 10)
	val N = readInt()
	println(A.indexOf(N) + 1)
}
STEP: 3
import scala.io.StdIn._

object Main extends App{
	val N = readLine().split(' ')(0).toInt
	val A = readLine().split(' ').map { _.toInt }
	println(A.indexOf(N) + 1)
}

Swift
STEP: 1
let A = [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]
let N = 8
print(A.firstIndex(of: N)! + 1)
STEP: 2
let A = [1, 5, 9, 7, 3, 2, 4, 8, 6, 10]
let N = Int(readLine()!)!
print(A.firstIndex(of: N)! + 1)
STEP: 3
let N = Int(readLine()!.split(separator: " ")[0])!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
print(A.firstIndex(of: N)! + 1)
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?