1
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ラーニングレベルアップ問題集の【配列を参照する操作】全ての要素に対する操作をやってみました。


問題


C
#include <stdio.h>

int main() {
	int n, k;
	scanf("%d %d", &n, &k);
	int A[n];
	for (int i = 0; i < n; i++) scanf("%d", &A[i]);
	int B[n];
	for (int i = 0; i < n; i++) B[i] = A[i] + k;
	for (int i = 0; i < n; i++) printf("%d\n", B[i]);
	return 0;
}

C++
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, k;
	cin >> n >> k;
	vector<int> A(n);
	for (int i = 0; i < n; i++) cin >> A[i];
	vector<int> B(n);
	for (int i = 0; i < n; i++) B[i] = A[i] + k;
	for (int i = 0; i < n; i++) cout << B[i] << endl;
	return 0;
}

C#
using System;
using System.Linq;

class Program
{
	static void Main()
	{
		int[] nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
		int n = nk[0];
		int k = nk[1];
		int[] A = Enumerable.Range(0, n).Select(_ => int.Parse(Console.ReadLine())).ToArray();
		Array.ForEach(A.Select(a => a + k).ToArray(), Console.WriteLine);
		// 別解
		// A.Select(a => a + k).ToList().ForEach(Console.WriteLine);
	}
}

Go
package main
import "fmt"

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

Java
import java.util.*;
import java.util.stream.IntStream;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] A = IntStream.range(0, n).map(i -> sc.nextInt()).toArray();
		Arrays.stream(A).map(a -> a + k).forEach(System.out::println);
	}
}

JavaScript
const [[n, k], ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? Number(s) : s.split(' ').map(Number));
A.map(a => a + k).forEach(a => console.log(a));

Kotlin
fun main() {
	val (n, k) = readLine()!!.split(' ').map { it.toInt() }
	val A = Array(n) { readLine()!!.toInt() }
	val B = A.map { it + k }
	B.forEach { println(it) }
}

PHP
<?php
	[$n, $k] = array_map("intval", explode(' ', fgets(STDIN)));
	$A = [];
	for ($i = 0; $i < $n; $i++) $A[] = intval(fgets(STDIN));
	foreach (array_map(fn($a) => $a + $k, $A) as $a) echo $a, PHP_EOL;
?>

Perl
my ($n, $k) = map { int($_) } split ' ', <STDIN>;
my @A;
for (1..$n) {
	push @A, int(<STDIN>);
}
for (map { $_ + $k } @A) {
	print "$_$/";
}

Python3
import numpy as np
n, k = map(int, input().split())
print(*np.array(list(int(input()) for _ in range(n))) + k, sep='\n')

Ruby
n, k = gets.split.map(&:to_i)
A = n.times.map { gets.to_i }
A.map { |a| a + k }.each do |a|
	p a
end

Scala
import scala.io.StdIn._

object Main extends App{
	val Array(n, k) = readLine().split(' ').map { _.toInt }
	val A = (0 until n).map { _ => readInt() }
	A.map { _ + k }.foreach(println)
}

Swift
let nk = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k) = (nk[0], nk[1])
var A: [Int] = []
for _ in 0..<n {
	A.append(Int(readLine()!)!)
}
A.map { $0 + k }.forEach { print($0) }
1
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
1
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?