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

Posted at

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


問題
配列の書き換え

2変数の入れ替え

配列の要素の入れ替え


C
配列の書き換え
#include <stdio.h>

int main() {
	int a, b, n;
	scanf("%d %d %d", &a, &b, &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] == a) A[i] = b;
	for (int i = 0; i < n; i++) printf("%d\n", A[i]);
	return 0;
}
2変数の入れ替え
#include <stdio.h>

void swap(int *a, int *b) {
	int t = *a;
	*a = *b;
	*b = t;
}

int main() {
	int a, b;
	scanf("%d %d", &a, &b);
	swap(&a, &b);
	printf("%d %d\n", a, b);
	return 0;
}
配列の要素の入れ替え
#include <stdio.h>

void swap(int *a, int *b) {
	int t = *a;
	*a = *b;
	*b = t;
}

int main() {
	int a, b, n;
	scanf("%d %d %d", &a, &b, &n);
	a -= 1;
	b -= 1;
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	swap(&A[a], &A[b]);
	for (int i = 0; i < n; i++) printf("%d\n", A[i]);
	return 0;
}

C++
配列の書き換え
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int a, b, n;
	cin >> a >> b >> 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] == a) A[i] = b;
	for (int i = 0; i < n; i++) cout << A[i] << endl;
	return 0;
}
2変数の入れ替え
#include <iostream>
#include <utility>
using namespace std;

int main() {
	int a, b;
	cin >> a >> b;
	swap(a, b);
	cout << a << ' ' << b << endl;
	return 0;
}
配列の要素の入れ替え
#include <iostream>
#include <vector>
#include <utility>
using namespace std;

int main() {
	int a, b, n;
	cin >> a >> b >> n;
	a -= 1;
	b -= 1;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	swap(A[a], A[b]);
	for (int i = 0; i < n; i++) cout << A[i] << endl;
	return 0;
}

C#
配列の書き換え
using System;

class Program
{
	public static void Main()
	{
		int[] abn = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = abn[0], b = abn[1], n = abn[2];
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		for (int i = 0; i < n; i++) if (A[i] == a) A[i] = b;
		for (int i = 0; i < n; i++) Console.WriteLine(A[i]);
	}
}
2変数の入れ替え
using System;

class Program
{
	private static void Swap(ref int a, ref int b) {
		int t = a;
		a = b;
		b = t;
	}
	
	public static void Main()
	{
		int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = ab[0], b = ab[1];
		Swap(ref a, ref b);
		Console.WriteLine("" + a + ' ' + b);
	}
}
配列の要素の入れ替え
using System;

class Program
{
	private static void Swap(ref int a, ref int b) {
		int t = a;
		a = b;
		b = t;
	}
	
	public static void Main()
	{
		int[] abn = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		int a = abn[0] - 1, b = abn[1] - 1, n = abn[2];
		int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		Swap(ref A[a], ref A[b]);
		for (int i = 0; i < n; i++) Console.WriteLine(A[i]);
	}
}

Go
配列の書き換え
package main
import "fmt"

func main() {
	var a, b, n int
	fmt.Scan(&a, &b, &n)
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	for i := 0; i < n; i++ {
		if A[i] == a {
			A[i] = b
		}
	}
	for i := 0; i < n; i++ {
		fmt.Println(A[i])
	}
}
2変数の入れ替え
package main
import "fmt"

func main() {
	var a, b int
	fmt.Scan(&a, &b)
	a, b = b, a
	fmt.Println(a, b)
}
配列の要素の入れ替え
package main
import "fmt"

func main() {
	var a, b, n int
	fmt.Scan(&a, &b, &n)
	a -= 1
	b -= 1
	A := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&A[i])
	}
	A[a], A[b] = A[b], A[a]
	for i := 0; i < n; i++ {
		fmt.Println(A[i])
	}
}

Java
配列の書き換え
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int n = sc.nextInt();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		for (int i = 0; i < n; i++) if (A[i] == a) A[i] = b;
		for (int i = 0; i < n; i++) System.out.println(A[i]);
	}
}
2変数の入れ替え
import java.util.*;

public class Main {

	private static void swap(int[] A) {
		int t = A[0];
		A[0] = A[1];
		A[1] = t;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] A = new int[2];
		A[0] = sc.nextInt();
		A[1] = sc.nextInt();
		sc.close();
		swap(A);
		System.out.println("" + A[0] + ' ' + A[1]);
	}
}
配列の要素の入れ替え
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt() - 1;
		int b = sc.nextInt() - 1;
		int n = sc.nextInt();
		sc.nextLine();
		int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int t = A[a];
		A[a] = A[b];
		A[b] = t;
		for (int i = 0; i < n; i++) System.out.println(A[i]);
	}
}

JavaScript
配列の書き換え
let [[a, b, n], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
for (var i = 0; i < n; i++) if (A[i] === a) A[i] = b;
for (var i = 0; i < n; i++) console.log(A[i]);
2変数の入れ替え
let [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
[a, b] = [b, a];
console.log(a, b);
配列の要素の入れ替え
let [[a, b, n], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
a -= 1;
b -= 1;
[A[a], A[b]] = [A[b], A[a]];
for (var i = 0; i < n; i++) console.log(A[i]);

Kotlin
配列の書き換え
fun main() {
	val (a, b, n) = readLine()!!.split(' ').map { it.toInt() }
	var A = readLine()!!.split(' ').map { it.toInt() }.toTypedArray()
	for (i in 0 until n) if (A[i] == a) A[i] = b
	for (i in 0 until n) println(A[i])
}
2変数の入れ替え
fun main() {
	var (a, b) = readLine()!!.split(' ').map { it.toInt() }
	val t = a
	a = b
	b = t
	println("" + a + ' ' + b)
}
配列の要素の入れ替え
fun main() {
	var (a, b, n) = readLine()!!.split(' ').map { it.toInt() }
	var A = readLine()!!.split(' ').map { it.toInt() }.toTypedArray()
	a -= 1
	b -= 1
	val t = A[a]
	A[a] = A[b]
	A[b] = t
	for (i in 0 until n) println(A[i])
}

PHP
配列の書き換え
<?php
	[$a, $b, $n] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	for ($i = 0; $i < $n; $i++) if ($A[$i] === $a) $A[$i] = $b;
	for ($i = 0; $i < $n; $i++) echo $A[$i], PHP_EOL;
?>
2変数の入れ替え
<?php
	[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
	[$a, $b] = [$b, $a];
	echo "$a $b", PHP_EOL;
?>
配列の要素の入れ替え
<?php
	[$a, $b, $n] = array_map("intval", explode(' ', fgets(STDIN)));
	$a -= 1;
	$b -= 1;
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	[$A[$a], $A[$b]] = [$A[$b], $A[$a]];
	for ($i = 0; $i < $n; $i++) echo $A[$i], PHP_EOL;
?>

Perl
配列の書き換え
my ($a, $b, $n) = map { int($_) } split ' ', <STDIN>;
my @A = map { int($_) } split ' ', <STDIN>;
for (my $i = 0; $i < $n; $i++) {
	if ($A[$i] == $a) {
		$A[$i] = $b;
	}
}
for (my $i = 0; $i < $n; $i++) {
	print "$A[$i]$/";
}
2変数の入れ替え
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
($a, $b) = ($b, $a);
print "$a $b$/";
配列の要素の入れ替え
my ($a, $b, $n) = map { int($_) } split ' ', <STDIN>;
$a -= 1;
$b -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
($A[$a], $A[$b]) = ($A[$b], $A[$a]);
for (my $i = 0; $i < $n; $i++) {
	print "$A[$i]$/";
}

Python3
配列の書き換え
a, b, n = map(int, input().split())
A = list(map(int, input().split()))
for i in range(n):
	if A[i] == a:
		A[i] = b
for i in range(n):
	print(A[i])
2変数の入れ替え
a, b = map(int, input().split())
a, b = b, a
print(a, b)
配列の要素の入れ替え
a, b, n = map(int, input().split())
a -= 1
b -= 1
A = list(map(int, input().split()))
A[a], A[b] = A[b], A[a]
for i in range(n):
	print(A[i])

Ruby
配列の書き換え
a, b, n = gets.split.map(&:to_i)
A = gets.split.map(&:to_i)
n.times do |i|
	if A[i] == a
		A[i] = b
	end
end
n.times do |i|
	p A[i]
end
2変数の入れ替え
a, b = gets.split.map(&:to_i)
a, b = b, a
print a, ' ', b, "\n"
配列の要素の入れ替え
a, b, n = gets.split.map(&:to_i)
a -= 1
b -= 1
A = gets.split.map(&:to_i)
A[a], A[b] = A[b], A[a]
n.times do |i|
	p A[i]
end

Scala
配列の書き換え
import scala.io.StdIn._

object Main extends App{
	val Array(a, b, n) = readLine().split(' ').map { _.toInt }
	var A = readLine().split(' ').map { _.toInt }
	for (i <- 0 until n) if (A(i) == a) A(i) = b
	for (i <- 0 until n) println(A(i))
}
2変数の入れ替え
import scala.io.StdIn._

object Main extends App{
	var Array(a, b) = readLine().split(' ').map { _.toInt }
	val t = a
	a = b
	b = t
	println("" + a + ' ' + b)
}
配列の要素の入れ替え
import scala.io.StdIn._

object Main extends App{
	var Array(a, b, n) = readLine().split(' ').map { _.toInt }
	a -= 1
	b -= 1
	var A = readLine().split(' ').map { _.toInt }
	val t = A(a)
	A(a) = A(b)
	A(b) = t
	for (i <- 0 until n) println(A(i))
}

Swift
配列の書き換え
let abn = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b, n) = (abn[0], abn[1], abn[2])
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
for i in 0..<n {
	if A[i] == a {
		A[i] = b
	}
}
for i in 0..<n {
	print(A[i])
}
2変数の入れ替え
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
var (a, b) = (ab[0], ab[1])
(a, b) = (b, a)
print(a, b)
配列の要素の入れ替え
let abn = readLine()!.split(separator: " ").compactMap { Int($0) }
var (a, b, n) = (abn[0], abn[1], abn[2])
a -= 1
b -= 1
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
(A[a], A[b]) = (A[b], A[a])
for i in 0..<n {
	print(A[i])
}
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?