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ラーニングレベルアップ問題集の【配列メニュー】をやってみた。(1)

Last updated at Posted at 2025-04-06

paizaラーニングレベルアップ問題集の【配列メニュー】をやってみました。


問題
要素数の出力

全要素の出力

i番目の出力


数列は全問

5, 1, 3, 4, 5, 12, 6, 8, 1, 3

です。

配列の添字は0始まりですので、$i$番目の要素を出力する時はA[i-1]とします。


C
要素数の出力
#include <stdio.h>

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	printf("%zu\n", sizeof(A) / sizeof(A[0]));
	return 0;
}
全要素の出力
#include <stdio.h>

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	int n = (int) (sizeof(A) / sizeof(A[0]));
	for (int i = 0; i < n; i++) printf("%d\n", A[i]);
	return 0;
}
i番目の出力
#include <stdio.h>

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	printf("%d\n", A[3]);
	return 0;
}

C++
要素数の出力
#include <iostream>
using namespace std;

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	cout << sizeof(A) / sizeof(A[0]) << endl;
	return 0;
}
全要素の出力
#include <iostream>
using namespace std;

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	for (int a : A) cout << a << endl;
	return 0;
}
i番目の出力
#include <iostream>
using namespace std;

const int A[] = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };

int main() {
	cout << A[3] << endl;
	return 0;
}

C#
要素数の出力
using System;

class Program
{
	static readonly int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };
	
	public static void Main()
	{
		Console.WriteLine(A.Length);
	}
}
全要素の出力
using System;

class Program
{
	static readonly int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };
	
	public static void Main()
	{
		foreach (int a in A) Console.WriteLine(a);
	}
}
i番目の出力
using System;

class Program
{
	static readonly int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };
	
	public static void Main()
	{
		Console.WriteLine(A[3]);
	}
}

Go
要素数の出力
package main
import "fmt"

var A = []int{ 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 }

func main() {
	fmt.Println(len(A))
}
全要素の出力
package main
import "fmt"

var A = []int{ 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 }

func main() {
	for _, a := range A {
		fmt.Println(a)
	}
}
i番目の出力
package main
import "fmt"

var A = []int{ 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 }

func main() {
	fmt.Println(A[3])
}

Java
要素数の出力
public class Main {

	static final int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 }
	
	public static void main(String[] args) {
		System.out.println(A.length);
	}
}
全要素の出力
public class Main {
	
	static final int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };
	
	public static void main(String[] args) {
		for (int a : A) System.out.println(a);
	}
}
i番目の出力
public class Main {
	
	static final int[] A = { 5, 1, 3, 4, 5, 12, 6, 8, 1, 3 };
	
	public static void main(String[] args) {
		System.out.println(A[3]);
	}
}

JavaScript
要素数の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
console.log(A.length);
全要素の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
A.forEach(a => console.log(a));
i番目の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
console.log(A[3]);

Kotlin
要素数の出力
fun main() {
	val A = arrayOf(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	println(A.size)
	// println(A.count()) # 別解
}
全要素の出力
fun main() {
	val A = arrayOf(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	for (a in A) println(a)
}
i番目の出力
fun main() {
	val A = arrayOf(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	println(A[3])
}

PHP
要素数の出力
<?php
	$A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
	echo count($A), PHP_EOL;
?>
全要素の出力
<?php
	$A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
	foreach ($A as $a) echo $a, PHP_EOL;
?>
i番目の出力
<?php
	$A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3];
	echo $A[3], PHP_EOL;
?>

Perl
要素数の出力
my @A = (5, 1, 3, 4, 5, 12, 6, 8, 1, 3);
print scalar(@A), $/;
全要素の出力
my @A = (5, 1, 3, 4, 5, 12, 6, 8, 1, 3);
for (@A) {
	print "$_$/"
}
i番目の出力
my @A = (5, 1, 3, 4, 5, 12, 6, 8, 1, 3);
print $A[3], $/;

Python3
要素数の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
print(len(A))
全要素の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
for a in A:
	print(a)
i番目の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
print(A[3])

Ruby
要素数の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
# 以下、どれでも正解
p A.size
p A.count
p A.length
全要素の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
A.each do |a|
	p a
end
i番目の出力
A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
p A[3]

Scala
要素数の出力
object Main extends App{
	val A = Array(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	println(A.size)
	// println(A.length) # 別解
}
全要素の出力
object Main extends App{
	val A = Array(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	for (a <- A) println(a)
}
i番目の出力
object Main extends App{
	val A = Array(5, 1, 3, 4, 5, 12, 6, 8, 1, 3)
	println(A(3))
}

Swift
要素数の出力
let A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
print(A.count)
全要素の出力
let A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
for a in A {
	print(a)
}
i番目の出力
let A = [5, 1, 3, 4, 5, 12, 6, 8, 1, 3]
print(A[3])
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?