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ラーニングレベルアップ問題集の「数列の和」「全ての要素の和」をやってみました。


問題
数列の和

全ての要素の和


  • 1問目の配列は半角空白区切り入力(ヨコ入力)
  • 2問目の配列は改行区切り入力(タテ入力)
    です。

C

2問共通です。

#include <stdio.h>

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

C++

2問共通です。

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

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

C#
数列の和
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		Console.ReadLine();
		Console.WriteLine(Console.ReadLine().Split().Select(_ => int.Parse(_)).Sum());
	}
}
全ての要素の和
using System;
using System.Linq;

class Program
{
	public static void Main()
	{
		Console.WriteLine(Enumerable.Range(0, int.Parse(Console.ReadLine())).Select(_ => int.Parse(Console.ReadLine())).Sum());
	}
}

Go

2問共通です。

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])
	}
	sum := 0
	for i := 0; i < n; i++ {
		sum += A[i]
	}
	fmt.Println(sum)
}

Java
数列の和
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		System.out.println(Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).sum());
		sc.close();
	}
}
全ての要素の和
import java.util.*;
import java.util.stream.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println(IntStream.range(0, sc.nextInt()).map(i -> sc.nextInt()).sum());
		sc.close();
	}
}

JavaScript
数列の和
const [n, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
console.log(A.reduce((a, b) => a + b, 0));
全ての要素の和
const [n, ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(Number);
console.log(A.reduce((a, b) => a + b, 0));

Kotlin
数列の和
fun main() {
	readLine()
	println(readLine()!!.split(' ').map { it.toInt() }.sum())
}
全ての要素の和
fun main() {
	println(Array(readLine()!!.toInt()) { readLine()!!.toInt() }.sum())
}

PHP
数列の和
<?php
	fgets(STDIN);
	echo array_sum(array_map("intval", explode(' ', fgets(STDIN)))), PHP_EOL;
?>
全ての要素の和
<?php
	$n = intval(fgets(STDIN));
	$A = [];
	for ($i = 0; $i < $n; $i++) $A[] = intval(fgets(STDIN));
	echo array_sum($A), PHP_EOL;
?>

Perl
数列の和
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my $sum = 0;
for (my $i = 0; $i < $n; $i++) {
	$sum += $A[$i]
}
print "$sum$/";
全ての要素の和
my $n = int(<STDIN>);
my @A;
for (my $i = 0; $i < $n; $i++) {
	push @A, int(<STDIN>);
}
my $sum = 0;
for (my $i = 0; $i < $n; $i++) {
	$sum += $A[$i]
}
print "$sum$/";

Python3
数列の和
input()
print(sum(list(map(int, input().split()))))
全ての要素の和
print(sum(int(input()) for _ in range(int(input()))))

Ruby
数列の和
gets
p gets.split.map(&:to_i).sum
全ての要素の和
p gets.to_i.times.map { gets.to_i }.sum

Scala
数列の和
import scala.io.StdIn._

object Main extends App{
	readInt()
	println(readLine().split(' ').map { _.toInt }.sum)
}
全ての要素の和
import scala.io.StdIn._

object Main extends App{
	println((0 until readInt()).map { _ => readInt() }.sum)
}

Swift
数列の和
let _ = readLine()
print(readLine()!.split(separator: " ").compactMap { Int($0) }.reduce(0, +))
全ての要素の和
var A: [Int] = []
for _ in 0..<Int(readLine()!)! {
	A.append(Int(readLine()!)!)
}
print(A.reduce(0, +))
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?