paizaラーニングレベルアップ問題集の「$N$が$M$ずつ増えたときにいつ$K$を越える?」「毎日増加するお金」をやってみました。
問題
NがMずつ増えたときにいつKを越える?
毎日増加するお金
方針
NがMずつ増えたときにいつKを越える?
\max\left(\left\lfloor\frac{K-N}{M}\right\rfloor+1,0\right)
ですが、今回はループを使って解きます。
毎日増加するお金
単に
\max\left(\left\lfloor\log_{1.1}\frac{B}{A}\right\rfloor+1,0\right)
では、「増加するお金は小数点以下切り捨て」という端数処理があるので、うまくいきません。ループを使います。
C
NがMずつ増えたときにいつKを越える?
#include <stdio.h>
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
int t = 0;
while (n <= k) {
n += m;
t++;
}
printf("%d\n", t);
return 0;
}
毎日増加するお金
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
int t = 0;
while (a <= b) {
a += a / 10;
t++;
}
printf("%d\n", t);
return 0;
}
C++
NがMずつ増えたときにいつKを越える?
#include <iostream>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int t = 0;
while (n <= k) {
n += m;
t++;
}
cout << t << endl;
return 0;
}
毎日増加するお金
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int t = 0;
while (a <= b) {
a += a / 10;
t++;
}
cout << t << endl;
return 0;
}
C#
NがMずつ増えたときにいつKを越える?
using System;
class Program
{
public static void Main()
{
int[] nmk = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int n = nmk[0], m = nmk[1], k = nmk[2];
int t = 0;
while (n <= k) {
n += m;
t++;
}
Console.WriteLine(t);
}
}
毎日増加するお金
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0], b = ab[1];
int t = 0;
while (a <= b) {
a += a / 10;
t++;
}
Console.WriteLine(t);
}
}
Go
NがMずつ増えたときにいつKを越える?
package main
import "fmt"
func main() {
var n, m, k int
fmt.Scan(&n, &m, &k)
t := 0
for n <= k {
n += m
t++
}
fmt.Println(t)
}
毎日増加するお金
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
t := 0
for a <= b {
a += a / 10
t++
}
fmt.Println(t)
}
Java
NがMずつ増えたときにいつKを越える?
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
sc.close();
int t = 0;
while (n <= k) {
n += m;
t++;
}
System.out.println(t);
}
}
毎日増加するお金
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 t = 0;
while (a <= b) {
a += a / 10;
t++;
}
System.out.println(t);
}
}
JavaScript
NがMずつ増えたときにいつKを越える?
let [n, m, k] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
let t = 0;
while (n <= k) {
n += m;
t++;
}
console.log(t);
毎日増加するお金
let [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
let t = 0;
while (a <= b) {
a += parseInt(a / 10);
t++;
}
console.log(t);
Kotlin
NがMずつ増えたときにいつKを越える?
fun main() {
var (n, m, k) = readLine()!!.split(' ').map { it.toInt() }
var t = 0
while (n <= k) {
n += m
t++
}
println(t)
}
毎日増加するお金
fun main() {
var (a, b) = readLine()!!.split(' ').map { it.toInt() }
var t = 0
while (a <= b) {
a += a / 10
t++
}
println(t)
}
PHP
NがMずつ増えたときにいつKを越える?
<?php
[$n, $m, $k] = array_map("intval", explode(' ', fgets(STDIN)));
$t = 0;
while ($n <= $k) {
$n += $m;
$t++;
}
echo $t, PHP_EOL;
?>
毎日増加するお金
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
$t = 0;
while ($a <= $b) {
$a += intdiv($a, 10);
$t++;
}
echo $t, PHP_EOL;
?>
Perl
NがMずつ増えたときにいつKを越える?
my ($n, $m, $k) = map { int($_) } split ' ', <STDIN>;
my $t = 0;
while ($n <= $k) {
$n += $m;
$t++;
}
print "$t$/";
毎日増加するお金
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
my $t = 0;
while ($a <= $b) {
$a += int($a / 10);
$t++;
}
print "$t$/";
Python3
NがMずつ増えたときにいつKを越える?
n, m, k = map(int, input().split())
t = 0
while n <= k:
n += m
t += 1
print(t)
毎日増加するお金
a, b = map(int, input().split())
t = 0
while a <= b:
a += a // 10
t += 1
print(t)
Ruby
NがMずつ増えたときにいつKを越える?
n, m, k = gets.split.map(&:to_i)
t = 0
while n <= k
n += m
t += 1
end
p t
毎日増加するお金
a, b = gets.split.map(&:to_i)
t = 0
while a <= b
a += a / 10
t += 1
end
p t
Scala
NがMずつ増えたときにいつKを越える?
import scala.io.StdIn._
object Main extends App{
var Array(n, m, k) = readLine().split(' ').map { _.toInt }
var t = 0
while (n <= k) {
n += m
t += 1
}
println(t)
}
毎日増加するお金
import scala.io.StdIn._
object Main extends App{
var Array(a, b) = readLine().split(' ').map { _.toInt }
var t = 0
while (a <= b) {
a += a / 10
t += 1
}
println(t)
}
Swift
NがMずつ増えたときにいつKを越える?
let nmk = readLine()!.split(separator: " ").compactMap { Int($0) }
var (n, m, k) = (nmk[0], nmk[1], nmk[2])
var t = 0
while n <= k {
n += m
t += 1
}
print(t)
毎日増加するお金
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
var (a, b) = (ab[0], ab[1])
var t = 0
while a <= b {
a += a / 10
t += 1
}
print(t)