paizaラーニングレベルアップ問題集の「変数/要素の入れ替え」をやってみました。
問題
2変数の入れ替え
変数の入れ替え
配列の要素の入れ替え
指定要素の入れ替え
方針
2変数の入れ替え
- 変数名は$A$,$B$です
- 入力値は1以上10以下です
変数の入れ替え
- 変数名は$X$,$Y$です
- 入力値は0以上100以下です
配列の要素の入れ替え
- 配列は半角空白区切り(ヨコ入力)で与えられます
- 配列の各要素は1以上10以下です
- 要素番号は最初に与えられます
指定要素の入れ替え
- 配列は改行区切り(タテ入力)で与えられます
- 配列の各要素は0以上100以下です
- 要素番号は最後に与えられます
C
2変数の入れ替え
#include <stdio.h>
void swap(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
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* x, int* y) {
int tmp = *x;
*x = *y;
*y = tmp;
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
swap(&x, &y);
printf("%d %d\n", x, y);
return 0;
}
配列の要素の入れ替え
#include <stdio.h>
void swap(int A[], int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
int main() {
int x, y, n;
scanf("%d %d %d", &x, &y, &n);
x -= 1;
y -= 1;
int A[n];
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
swap(A, x, y);
for (int i = 0; i < n; i++) printf("%d\n", A[i]);
return 0;
}
指定要素の入れ替え
#include <stdio.h>
void swap(int A[], int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
int main() {
int n;
scanf("%d", &n);
int A[n];
for (int i = 0; i < n; i++) scanf("%d", &A[i]);
int x, y;
scanf("%d %d", &x, &y);
x -= 1;
y -= 1;
swap(A, x, y);
for (int i = 0; i < n; i++) printf("%d\n", A[i]);
return 0;
}
C++
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 <utility>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
swap(x, y);
cout << x << ' ' << y << endl;
return 0;
}
配列の要素の入れ替え
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
int x, y, n;
cin >> x >> y >> n;
x -= 1;
y -= 1;
vector<int> A(n);
for (int i = 0; i < n; i++) cin >> A[i];
swap(A[x], A[y]);
for (int i = 0; i < n; i++) cout << A[i] << endl;
return 0;
}
指定要素の入れ替え
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> A(n);
for (int i = 0; i < n; i++) cin >> A[i];
int x, y;
cin >> x >> y;
x -= 1;
y -= 1;
swap(A[x], A[y]);
for (int i = 0; i < n; i++) cout << A[i] << endl;
return 0;
}
C#
2変数の入れ替え
using System;
class Program
{
static void Swap(ref int a, ref int b) {
int tmp = a;
a = b;
b = tmp;
}
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
{
static void Swap(ref int x, ref int y) {
int tmp = x;
x = y;
y = tmp;
}
public static void Main()
{
int[] xy = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int x = xy[0], y = xy[1];
Swap(ref x, ref y);
Console.WriteLine("" + x + ' ' + y);
}
}
配列の要素の入れ替え
using System;
class Program
{
private static void Swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
public static void Main()
{
int[] xyn = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int x = xyn[0] - 1, y = xyn[1] - 1;
int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
Swap(A, x, y);
foreach (int a in A) Console.WriteLine(a);
}
}
指定要素の入れ替え
using System;
using System.Linq;
class Program
{
private static void Swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
public static void Main()
{
int[] A = Enumerable.Range(0, int.Parse(Console.ReadLine())).Select(_ => int.Parse(Console.ReadLine())).ToArray();
int[] xy = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int x = xy[0] - 1, y = xy[1] - 1;
Swap(A, x, y);
foreach (int a in A) Console.WriteLine(a);
}
}
Go
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 x, y int
fmt.Scan(&x, &y)
x, y = y, x
fmt.Println(x, y)
}
配列の要素の入れ替え
package main
import "fmt"
func main() {
var x, y, n int
fmt.Scan(&x, &y, &n)
x -= 1
y -= 1
A := make([]int, n)
for i := 0; i < n; i++ {
fmt.Scan(&A[i])
}
A[x], A[y] = A[y], A[x]
for i := 0; i < n; i++ {
fmt.Println(A[i])
}
}
指定要素の入れ替え
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
A := make([]int, n)
for i := 0; i < n; i++ {
fmt.Scan(&A[i])
}
var x, y int
fmt.Scan(&x, &y)
x -= 1
y -= 1
A[x], A[y] = A[y], A[x]
for i := 0; i < n; i++ {
fmt.Println(A[i])
}
}
Java
2変数の入れ替え
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();
sc.close();
int tmp = a;
a = b;
b = tmp;
System.out.println("" + a + ' ' + b);
}
}
変数の入れ替え
無理矢理swap
関数を作ってみました。
import java.util.*;
public class Main {
private static void swap(int[] X, int[] Y) {
int tmp = X[0];
X[0] = Y[0];
Y[0] = tmp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
sc.close();
int[] X = {x};
int[] Y = {y};
swap(X, Y);
System.out.println("" + X[0] + ' ' + Y[0]);
}
}
配列の要素の入れ替え
import java.util.*;
public class Main {
private static void swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
int n = sc.nextInt();
sc.nextLine();
int[] A = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
sc.close();
swap(A, x, y);
for (int i = 0; i < n; i++) System.out.println(A[i]);
}
}
指定要素の入れ替え
import java.util.*;
import java.util.stream.IntStream;
public class Main {
private static void swap(int[] A, int i, int j) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] A = IntStream.range(0, n).map(i -> sc.nextInt()).toArray();
int x = sc.nextInt() - 1;
int y = sc.nextInt() - 1;
sc.close();
swap(A, x, y);
for (int i = 0; i < n; i++) System.out.println(A[i]);
}
}
JavaScript
2変数の入れ替え
let [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
[a, b] = [b, a];
console.log(a, b);
変数の入れ替え
let [x, y] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
[x, y] = [y, x];
console.log(x, y);
配列の要素の入れ替え
let [[x, y, n], A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
x -= 1;
y -= 1;
[A[x], A[y]] = [A[y], A[x]];
for (var i = 0; i < n; i++) console.log(A[i]);
指定要素の入れ替え
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const n = Number(lines.shift());
const [x, y] = lines.pop().split(' ').map(s => Number(s) - 1);
const A = lines.map(Number);
[A[x], A[y]] = [A[y], A[x]];
for (var i = 0; i < n; i++) console.log(A[i]);
Kotlin
2変数の入れ替え
fun main() {
var (a, b) = readLine()!!.split(' ').map { it.toInt() }
val tmp = a
a = b
b = tmp
println("" + a + ' ' + b)
}
変数の入れ替え
fun main() {
var (x, y) = readLine()!!.split(' ').map { it.toInt() }
val tmp = x
x = y
y = tmp
println("" + x + ' ' + y)
}
配列の要素の入れ替え
fun main() {
var (x, y, n) = readLine()!!.split(' ').map { it.toInt() }
x -= 1
y -= 1
val A = readLine()!!.split(' ').map { it.toInt() }.toTypedArray()
A[x] = A[y].also { A[y] = A[x] }
for (i in 0 until n) println(A[i])
}
指定要素の入れ替え
fun main() {
val n = readLine()!!.toInt()
val A = Array(n) { readLine()!!.toInt() }
val (x, y) = readLine()!!.split(' ').map { it.toInt() - 1 }
A[x] = A[y].also { A[y] = A[x] }
for (i in 0 until n) println(A[i])
}
PHP
2変数の入れ替え
<?php
[$a, $b] = array_map("intval", explode(' ', trim(fgets(STDIN))));
[$a, $b] = [$b, $a];
echo $a, ' ', $b, PHP_EOL;
?>
変数の入れ替え
<?php
[$x, $y] = array_map("intval", explode(' ', trim(fgets(STDIN))));
[$x, $y] = [$y, $x];
echo $x, ' ', $y, PHP_EOL;
?>
配列の要素の入れ替え
<?php
[$x, $y, $n] = array_map("intval", explode(' ', trim(fgets(STDIN))));
$x -= 1;
$y -= 1;
$A = array_map("intval", explode(' ', trim(fgets(STDIN))));
[$A[$x], $A[$y]] = [$A[$y], $A[$x]];
for ($i = 0; $i < $n; $i++) echo $A[$i], PHP_EOL;
?>
指定要素の入れ替え
<?php
$n = intval(fgets(STDIN));
$A = [];
for ($i = 0; $i < $n; $i++) $A[] = intval(fgets(STDIN));
[$x, $y] = array_map(fn($s) => intval($s) - 1, explode(' ', trim(fgets(STDIN))));
[$A[$x], $A[$y]] = [$A[$y], $A[$x]];
for ($i = 0; $i < $n; $i++) echo $A[$i], PHP_EOL;
?>
Perl
2変数の入れ替え
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
($a, $b) = ($b, $a);
print "$a $b$/";
変数の入れ替え
my ($x, $y) = map { int($_) } split ' ', <STDIN>;
($x, $y) = ($y, $x);
print "$x $y$/";
配列の要素の入れ替え
my ($x, $y, $n) = map { int($_) } split ' ', <STDIN>;
$x -= 1;
$y -= 1;
my @A = map { int($_) } split ' ', <STDIN>;
($A[$x], $A[$y]) = ($A[$y], $A[$x]);
for ($i = 0; $i < $n; $i++) {
print $A[$i], $/;
}
指定要素の入れ替え
my $n = int(<STDIN>);
my @A;
for (1..$n) {
push @A, int(<STDIN>);
}
my ($x, $y) = map { int($_) - 1 } split ' ', <STDIN>;
($A[$x], $A[$y]) = ($A[$y], $A[$x]);
for ($i = 0; $i < $n; $i++) {
print $A[$i], $/;
}
Python3
2変数の入れ替え
a, b = map(int, input().split())
a, b = b, a
print(a, b)
変数の入れ替え
x, y = map(int, input().split())
x, y = y, x
print(x, y)
配列の要素の入れ替え
x, y, n = map(int, input().split())
x -= 1
y -= 1
A = list(map(int, input().split()))
A[x], A[y] = A[y], A[x]
for i in range(n):
print(A[i])
指定要素の入れ替え
n = int(input())
A = [int(input()) for _ in range(n)]
x, y = map(lambda s: int(s) - 1, input().split())
A[x], A[y] = A[y], A[x]
for i in range(n):
print(A[i])
Ruby
2変数の入れ替え
a, b = gets.split.map(&:to_i)
a, b = b, a
print a, ' ', b, "\n"
変数の入れ替え
x, y = gets.split.map(&:to_i)
x, y = y, x
print x, ' ', y, "\n"
配列の要素の入れ替え
x, y, n = gets.split.map(&:to_i)
x -= 1
y -= 1
A = gets.split.map(&:to_i)
A[x], A[y] = A[y], A[x]
n.times do |i|
p A[i]
end
指定要素の入れ替え
n = gets.to_i
A = n.times.map { gets.to_i }
x, y = gets.split.map { |s| s.to_i - 1 }
A[x], A[y] = A[y], A[x]
n.times do |i|
p A[i]
end
Scala
2変数の入れ替え
import scala.io.StdIn._
object Main extends App{
var Array(a, b) = readLine().split(' ').map { _.toInt }
val tmp = a
a = b
b = tmp
println("" + a + ' ' + b)
}
変数の入れ替え
import scala.io.StdIn._
object Main extends App{
var Array(x, y) = readLine().split(' ').map { _.toInt }
val tmp = x
x = y
y = tmp
println("" + x + ' ' + y)
}
配列の要素の入れ替え
import scala.io.StdIn._
object Main extends App{
def swap(A: Array[Int], x: Int, y: Int) = {
val tmp = A(x)
A(x) = A(y)
A(y) = tmp
}
var Array(x, y, n) = readLine().split(' ').map { _.toInt }
x -= 1
y -= 1
val A = readLine().split(' ').map { _.toInt }
swap(A, x, y)
for (i <- 0 until n) println(A(i))
}
指定要素の入れ替え
import scala.io.StdIn._
object Main extends App{
def swap(A: Array[Int], x: Int, y: Int) = {
val tmp = A(x)
A(x) = A(y)
A(y) = tmp
}
val n = readInt()
val A = (0 until n).map { _ => readInt() }.toArray
var Array(x, y) = readLine().split(' ').map { _.toInt - 1 }
swap(A, x, y)
for (i <- 0 until n) println(A(i))
}
Swift
2変数の入れ替え
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
var (a, b) = (ab[0], ab[1])
(a, b) = (b, a)
print(a, b)
変数の入れ替え
let xy = readLine()!.split(separator: " ").compactMap { Int($0) }
var (x, y) = (xy[0], xy[1])
(x, y) = (y, x)
print(x, y)
配列の要素の入れ替え
let xyn = readLine()!.split(separator: " ").compactMap { Int($0) }
let (x, y, n) = (nxy[0] - 1, nxy[1] - 1, nxy[2])
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
(A[x], A[y]) = (A[y], A[x])
for i in 0..<n {
print(A[i])
}
指定要素の入れ替え
let n = Int(readLine()!)!
var A = (0..<n).map { _ in Int(readLine()!)! }
let xy = readLine()!.split(separator: " ").compactMap { Int($0) }
let (x, y) = (xy[0] - 1, xy[1] - 1)
(A[x], A[y]) = (A[y], A[x])
for i in 0..<n {
print(A[i])
}