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桁ずつ計算する方法もあります(公式解説別解)が、今回はwhile文を使って

  • $N$を$10$で割った余りを足す
  • $N$を$10$で割った商に置き換える
  • $N$が$0$になったら終了
    という方針で解いてみたいと思います。
カウント変数を使った計算
import numpy as np
print(*(np.arange(int(input())) + 1) * np.array(list(map(int, input().split()))), sep='\n')

ですが、今回はfor文を使います。
配列要素の添字と掛ける数が1ずれることに注意しましょう。


C
各桁の和
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	int digit_sum = 0;
	while (n) {
		digit_sum += n % 10;
		n /= 10;
	}
	printf("%d\n", digit_sum);
	return 0;
}
カウント変数を使った計算
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		int m;
		scanf("%d", &m);
		printf("%d\n", m * i);
	}
	return 0;
}

C++
各桁の和
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	int digit_sum = 0;
	while (n) {
		digit_sum += n % 10;
		n /= 10;
	}
	cout << digit_sum << endl;
	return 0;
}
カウント変数を使った計算
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int m;
		cin >> m;
		cout << m * i << endl;
	}
	return 0;
}

C#
各桁の和
using System;

class Program
{
	public static void Main()
	{
		int n = int.Parse(Console.ReadLine());
		int digit_sum = 0;
		while (n > 0) {
			digit_sum += n % 10;
			n /= 10;
		}
		Console.WriteLine(digit_sum);
	}
}
カウント変数を使った計算
using System;

class Program
{
	public static void Main()
	{
		int N = int.Parse(Console.ReadLine());
		int[] M = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
		for (int i = 1; i <= N; i++) Console.WriteLine(M[i - 1] * i);
	}
}

Go
各桁の和
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	digit_sum := 0
	for n > 0 {
		digit_sum += n % 10
		n /= 10
	}
	fmt.Println(digit_sum)
}
カウント変数を使った計算
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var m int
		fmt.Scan(&m)
		fmt.Println(m * i)
	}
	var s string
	fmt.Scan(&s)
	fmt.Println(s)
}

Java
各桁の和
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();
		int digit_sum = 0;
		while (n > 0) {
			digit_sum += n % 10;
			n /= 10;
		}
		System.out.println(digit_sum);
	}
}
カウント変数を使った計算
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 1; i <= n; i++) System.out.println(sc.nextInt() * i);
		sc.close();
	}
}

JavaScript
各桁の和
var n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
var digit_sum = 0;
while (n) {
	digit_sum += n % 10;
	n = parseInt(n / 10);
}
console.log(digit_sum);
カウント変数を使った計算
const [N, M] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map((s, i) => i ? s.split(' ').map(Number) : Number(s));
for (var i = 1; i <= N; i++) console.log(M[i - 1] * i);

Kotlin
各桁の和
fun main() {
	var n = readLine()!!.toInt()
	var digit_sum = 0
	while (n > 0) {
		digit_sum += n % 10
		n /= 10
	}
	println(digit_sum)
}
カウント変数を使った計算
fun main() {
	val n = readLine()!!.toInt()
	val M = readLine()!!.split(' ').map { it.toInt() }
	for (i in 1..n) println(M[i - 1] * i)
}

PHP
各桁の和
<?php
	$n = intval(fgets(STDIN));
	$digit_sum = 0;
	while ($n) {
		$digit_sum += $n % 10;
		$n = intdiv($n, 10);
	}
	echo $digit_sum, PHP_EOL;
?>
カウント変数を使った計算
<?php
	$n = intval(fgets(STDIN));
	$A = array_map("intval", explode(' ', fgets(STDIN)));
	for ($i = 1; $i <= $n; $i++) echo $A[$i - 1] * $i, PHP_EOL;
?>

Perl
各桁の和
my $n = int(<STDIN>);
my $digit_sum = 0;
while ($n) {
	$digit_sum += $n % 10;
	$n = int($n / 10);
}
print "$digit_sum$/";
カウント変数を使った計算
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
for (1..$n) {
	print $A[$_ - 1] * $_, $/;
}

Python3
各桁の和
n = int(input())
digit_sum = 0
while n:
	digit_sum += n % 10
	n //= 10
print(digit_sum)
カウント変数を使った計算
n = int(input())
A = list(map(int, input().split()))
for i in range(n):
	print(A[i] * (i + 1))

Ruby
各桁の和
n = gets.to_i
digit_sum = 0
while n > 0
	digit_sum += n % 10
	n /= 10
end
p digit_sum
カウント変数を使った計算
N = gets.to_i
A = gets.split.map(&:to_i)
N.times do |i|
	p A[i] * (i + 1)
end

Scala
各桁の和
import scala.io.StdIn._

object Main extends App{
	var n = readInt()
	var digit_sum = 0
	while (n > 0) {
		digit_sum += n % 10
		n /= 10
	}
	println(digit_sum)
}
カウント変数を使った計算
import scala.io.StdIn._

object Main extends App{
	val n = readInt()
	val A = readLine().split(' ').map { _.toInt }
	for (i <- 1 to n) println(A(i - 1) * i)
}

Swift
各桁の和
var n = Int(readLine()!)!
var digit_sum = 0
while n > 0 {
	digit_sum += n % 10
	n /= 10
}
print(digit_sum)
カウント変数を使った計算
let n = Int(readLine()!)!
var A = readLine()!.split(separator: " ").compactMap { Int($0) }
for i in 1...n {
	print(A[i - 1] * 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?