paizaラーニングレベルアップ問題集の【条件分岐メニュー】をやってみました。
問題
池の周回
崖に落ちるか判定
タイルの敷き詰め
積の最小化
方針
池の周回
$K\times T$が$N$の倍数ならば、丁度スタート地点に戻ってきています。
崖に落ちるか判定
$K\times N$が$T$以下ならば、崖に落ちません。
タイルの敷き詰め
$H$, $W$が共に$2$の倍数ならば、箱に隙間なくタイルを敷き詰めることができます。
積の最小化
- $0\le A\le B$の場合
$A\times A$が最小です。 - $A<0\le B$の場合
$A\times B$が最小です。 - $A\le B< 0$の場合
$B\times B$が最小です。
C
池の周回
#include <stdio.h>
int main() {
int n, k, t;
scanf("%d %d %d", &n, &k, &t);
puts(k * t % n ? "NO" : "YES");
return 0;
}
崖に落ちるか判定
#include <stdio.h>
int main() {
int n, k, t;
scanf("%d %d %d", &n, &k, &t);
puts(k * n <= t ? "YES" : "NO");
return 0;
}
タイルの敷き詰め
#include <stdio.h>
int main() {
int h, w;
scanf("%d %d", &h, &w);
puts(h % 2 || w % 2 ? "NO" : "YES");
return 0;
}
積の最小化
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
if (a > 0) printf("%d\n", a * a);
else if (b > 0) printf("%d\n", a * b);
else printf("%d\n",b * b);
return 0;
}
C++
池の周回
#include <iostream>
using namespace std;
int main() {
int n, k, t;
cin >> n >> k >> t;
cout << (k * t % n ? "NO" : "YES") << endl;
return 0;
}
崖に落ちるか判定
#include <iostream>
using namespace std;
int main() {
int n, k, t;
cin >> n >> k >> t;
cout << (k * n <= t ? "YES" : "NO") << endl;
return 0;
}
タイルの敷き詰め
#include <iostream>
using namespace std;
int main() {
int h, w;
cin >> h >> w;
cout << (h % 2 || w % 2 ? "NO" : "YES") << endl;
return 0;
}
積の最小化
#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > 0) cout << a * a << endl;
else if (b > 0) cout << a * b << endl;
else cout << b * b << endl;
return 0;
}
C#
池の周回
using System;
class Program
{
public static void Main()
{
int[] nkt = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int n = nkt[0];
int k = nkt[1];
int t = nkt[2];
Console.WriteLine(k * t % n == 0 ? "YES" : "NO");
}
}
崖に落ちるか判定
using System;
class Program
{
public static void Main()
{
int[] nkt = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int n = nkt[0];
int k = nkt[1];
int t = nkt[2];
Console.WriteLine(k * n <= t ? "YES" : "NO");
}
}
タイルの敷き詰め
using System;
class Program
{
public static void Main()
{
int[] hw = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int h = hw[0];
int w = hw[1];
Console.WriteLine(h % 2 == 0 && w % 2 == 0 ? "YES" : "NO");
}
}
積の最小化
using System;
class Program
{
public static void Main()
{
int[] ab = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int a = ab[0];
int b = ab[1];
if (a > 0) Console.WriteLine(a * a);
else if (b > 0) Console.WriteLine(a * b);
else Console.WriteLine(b * b);
}
}
Go
池の周回
package main
import "fmt"
func main() {
var n, k, t int
fmt.Scan(&n, &k, &t)
if k * t % n == 0 {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
崖に落ちるか判定
package main
import "fmt"
func main() {
var n, k, t int
fmt.Scan(&n, &k, &t)
if k * n <= t {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
タイルの敷き詰め
package main
import "fmt"
func main() {
var h, w int
fmt.Scan(&h, &w)
if h % 2 == 0 && w % 2 == 0 {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
積の最小化
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
if a > 0 {
fmt.Println(a * a)
} else if b > 0 {
fmt.Println(a * b)
} else {
fmt.Println(b * b)
}
}
Java
池の周回
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int t = sc.nextInt();
sc.close();
System.out.println(k * t % n == 0 ? "YES" : "NO");
}
}
崖に落ちるか判定
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int t = sc.nextInt();
sc.close();
System.out.println(k * n <= t ? "YES" : "NO");
}
}
タイルの敷き詰め
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int w = sc.nextInt();
sc.close();
System.out.println(h % 2 == 0 && w % 2 == 0 ? "YES" : "NO");
}
}
積の最小化
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();
if (a > 0) System.out.println(a * a);
else if (b > 0) System.out.println(a * b);
else System.out.println(b * b);
}
}
JavaScript
池の周回
const [n, k, t] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(k * t % n ? "NO" : "YES");
崖に落ちるか判定
const [n, k, t] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(k * n <= t ? "YES" : "NO");
タイルの敷き詰め
const [h, w] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
console.log(h % 2 || w % 2 ? "NO" : "YES");
積の最小化
const [a, b] = require("fs").readFileSync("/dev/stdin", "utf8").split(' ').map(Number);
if (a > 0) console.log(a * a);
else if (b > 0) console.log(a * b);
else console.log(b * b);
Kotlin
池の周回
fun main() {
val (n, k, t) = readLine()!!.split(' ').map { it.toInt() }
println(if (k * t % n == 0) "YES" else "NO")
}
崖に落ちるか判定
fun main() {
val (n, k, t) = readLine()!!.split(' ').map { it.toInt() }
println(if (k * n <= t) "YES" else "NO")
}
タイルの敷き詰め
fun main() {
val (h, w) = readLine()!!.split(' ').map { it.toInt() }
println(if (h % 2 == 0 && w % 2 == 0) "YES" else "NO")
}
積の最小化
fun main() {
val (a, b) = readLine()!!.split(' ').map { it.toInt() }
if (a > 0) println(a * a)
else if (b > 0) println(a * b)
else println(b * b)
}
PHP
池の周回
<?php
[$n, $k, $t] = array_map("intval", explode(' ', fgets(STDIN)));
echo $k * $t % $n ? "NO" : "YES", PHP_EOL;
?>
崖に落ちるか判定
<?php
[$n, $k, $t] = array_map("intval", explode(' ', fgets(STDIN)));
echo $k * $n <= $t ? "YES" : "NO", PHP_EOL;
?>
タイルの敷き詰め
<?php
[$h, $w] = array_map("intval", explode(' ', fgets(STDIN)));
echo $h % 2 || $w % 2 ? "NO" : "YES", PHP_EOL;
?>
積の最小化
<?php
[$a, $b] = array_map("intval", explode(' ', fgets(STDIN)));
if ($a > 0) echo $a * $a, PHP_EOL;
else if ($b > 0) echo $a * $b, PHP_EOL;
else echo $b * $b, PHP_EOL;
?>
Perl
池の周回
my ($n, $k, $t) = map { int($_) } split ' ', <STDIN>;
print $k * $t % $n ? "NO$/" : "YES$/";
崖に落ちるか判定
my ($n, $k, $t) = map { int($_) } split ' ', <STDIN>;
print $k * $n <= $t ? "YES$/" : "NO$/";
タイルの敷き詰め
my ($h, $w) = map { int($_) } split ' ', <STDIN>;
print $h % 2 || $w % 2 ? "NO$/" : "YES$/";
積の最小化
my ($a, $b) = map { int($_) } split ' ', <STDIN>;
if ($a > 0) {
print $a * $a, $/;
} elsif ($b > 0) {
print $a * $b, $/;
} else {
print $b * $b, $/;
}
Python3
池の周回
n, k, t = map(int, input().split())
print("NO" if k * t % n else "YES")
崖に落ちるか判定
n, k, t = map(int, input().split())
print("YES" if k * n <= t else "NO")
タイルの敷き詰め
h, w = map(int, input().split())
print("NO" if h % 2 or w % 2 else "YES")
積の最小化
a, b = map(int, input().split())
if a > 0:
print(a * a)
elif b > 0:
print(a * b)
else:
print(b * b)
Ruby
池の周回
n, k, t = gets.split.map(&:to_i)
puts k * t % n == 0 ? "YES" : "NO"
崖に落ちるか判定
n, k, t = gets.split.map(&:to_i)
puts k * n <= t ? "YES" : "NO"
タイルの敷き詰め
h, w = gets.split.map(&:to_i)
puts h % 2 == 0 && w % 2 == 0 ? "YES" : "NO"
積の最小化
a, b = gets.split.map(&:to_i)
if a > 0
p a * a
elsif b > 0
p a * b
else
p b * b
end
Scala
池の周回
import scala.io.StdIn._
object Main extends App{
val Array(n, k, t) = readLine().split(' ').map { _.toInt }
println(if (k * t % n == 0) "YES" else "NO")
}
崖に落ちるか判定
import scala.io.StdIn._
object Main extends App{
val Array(n, k, t) = readLine().split(' ').map { _.toInt }
println(if (k * n <= t) "YES" else "NO")
}
タイルの敷き詰め
import scala.io.StdIn._
object Main extends App{
val Array(h, w) = readLine().split(' ').map { _.toInt }
println(if (h % 2 == 0 && w % 2 == 0) "YES" else "NO")
}
積の最小化
import scala.io.StdIn._
object Main extends App{
val Array(a, b) = readLine().split(' ').map { _.toInt }
if (a > 0) println(a * a)
else if (b > 0) println(a * b)
else println(b * b)
}
Swift
池の周回
let nkt = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k, t) = (nkt[0], nkt[1], nkt[2])
print(k * t % n == 0 ? "YES" : "NO")
崖に落ちるか判定
let nkt = readLine()!.split(separator: " ").compactMap { Int($0) }
let (n, k, t) = (nkt[0], nkt[1], nkt[2])
print(k * n <= t ? "YES" : "NO")
タイルの敷き詰め
let hw = readLine()!.split(separator: " ").compactMap { Int($0) }
let (h, w) = (hw[0], hw[1])
print(h % 2 == 0 && w % 2 == 0 ? "YES" : "NO")
積の最小化
let ab = readLine()!.split(separator: " ").compactMap { Int($0) }
let (a, b) = (ab[0], ab[1])
if a > 0 {
print(a * a)
} else if b > 0 {
print(a * b)
} else {
print(b * b)
}