paizaラーニングレベルアップ問題集の【条件分岐メニュー】をやってみました。
問題
3の倍数判定
2つの倍数判定
偶奇の判定
曜日の判定
FizzBuzz
else-if
文を扱う前にswitch
文を扱っております。(出題者の意図としてはelse-if
を使うことを想定していたかもしれませんが)
C
3の倍数判定
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 3 == 0) {
puts("YES");
} else {
puts("NO");
}
return 0;
}
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
puts(n % 3 ? "NO" : "YES");
return 0;
}
2つの倍数判定
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 3 == 0 && n % 5 == 0) {
puts("YES");
} else {
puts("NO");
}
return 0;
}
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
puts(n % 3 || n % 5 ? "NO" : "YES");
return 0;
}
偶奇の判定
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
if (a % 2 == 0) {
even++;
} else {
odd++;
}
}
printf("%d %d\n", even, odd);
return 0;
}
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int count[2] = {};
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
count[a%2]++;
}
printf("%d %d\n", count[0], count[1]);
return 0;
}
曜日の判定
#include <stdio.h>
int main() {
int x;
scanf("%d", &x);
switch (x % 7) {
case 1:
puts("Sun");
break;
case 2:
puts("Mon");
break;
case 3:
puts("Tue");
break;
case 4:
puts("Wed");
break;
case 5:
puts("Thu");
break;
case 6:
puts("Fri");
break;
default:
puts("Sat");
}
return 0;
}
#include <stdio.h>
const char* WDAY[] = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"};
int main() {
int x;
scanf("%d", &x);
puts(WDAY[x%7]);
return 0;
}
FizzBuzz
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 3 == 0) {
if (n % 5 == 0) {
puts("FizzBuzz");
} else {
puts("Fizz");
}
} else {
if (n % 5 == 0) {
puts("Buzz");
} else {
printf("%d\n", n);
}
}
return 0;
}
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
return (n%3?n%5?printf("%d\n", n):puts("Buzz"):puts(n%5?"Fizz":"FizzBuzz")) * 0;
}
C++
3の倍数判定
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 3 == 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n % 3 ? "NO" : "YES") << endl;
return 0;
}
2つの倍数判定
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 3 == 0 && n % 5 == 0) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n % 3 || n % 5 ? "NO" : "YES") << endl;
return 0;
}
偶奇の判定
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (a % 2 == 0) {
even++;
} else {
odd++;
}
}
cout << even << ' ' << odd << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int count[2] = {};
while (n--) {
int a;
cin >> a;
count[a % 2]++;
}
cout << count[0] << ' ' << count[1] << endl;
return 0;
}
曜日の判定
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
switch (x % 7) {
case 1:
cout << "Sun" << endl;
break;
case 2:
cout << "Mon" << endl;
break;
case 3:
cout << "Tue" << endl;
break;
case 4:
cout << "Wed" << endl;
break;
case 5:
cout << "Thu" << endl;
break;
case 6:
cout << "Fri" << endl;
break;
default:
cout << "Sat" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
const string WDAY[] = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" };
int main() {
int x;
cin >> x;
cout << WDAY[x%7] << endl;
}
FizzBuzz
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 3 == 0) {
if (n % 5 == 0) {
cout << "FizzBuzz" << endl;
} else {
cout << "Fizz" << endl;
}
} else {
if (n % 5 == 0) {
cout << "Buzz" << endl;
} else {
cout << n << endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
cout << (n%3?n%5?to_string(n):"Buzz":n%5?"Fizz":"FizzBuzz") << endl;
return 0;
}
C#
3の倍数判定
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
if (n % 3 == 0) {
Console.WriteLine("YES");
} else {
Console.WriteLine("NO");
}
}
}
using System;
class Program
{
public static void Main()
{
Console.WriteLine(int.Parse(Console.ReadLine()) % 3 == 0 ? "YES" : "NO");
}
}
2つの倍数判定
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
if (n % 3 == 0 && n % 5 == 0) {
Console.WriteLine("YES");
} else {
Console.WriteLine("NO");
}
}
}
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine(n % 3 == 0 && n % 5 == 0 ? "YES" : "NO");
}
}
偶奇の判定
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
int[] A = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
int even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
if (A[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
Console.WriteLine("" + even + ' ' + odd);
}
}
using System;
class Program
{
public static void Main()
{
int[] count = new int[2];
int n = int.Parse(Console.ReadLine());
foreach (int a in Array.ConvertAll(Console.ReadLine().Split(), int.Parse)) count[a%2]++;
Console.WriteLine("" + count[0] + ' ' + count[1]);
}
}
曜日の判定
using System;
class Program
{
public static void Main()
{
int x = int.Parse(Console.ReadLine());
switch (x % 7) {
case 1:
Console.WriteLine("Sun");
break;
case 2:
Console.WriteLine("Mon");
break;
case 3:
Console.WriteLine("Tue");
break;
case 4:
Console.WriteLine("Wed");
break;
case 5:
Console.WriteLine("Thu");
break;
case 6:
Console.WriteLine("Fri");
break;
default:
Console.WriteLine("Sat");
break;
}
}
}
using System;
class Program
{
static readonly string[] WDAY = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" };
public static void Main()
{
Console.WriteLine(WDAY[int.Parse(Console.ReadLine()) % 7]);
}
}
FizzBuzz
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
if (n % 3 == 0) {
if (n % 5 == 0) {
Console.WriteLine("FizzBuzz");
} else {
Console.WriteLine("Fizz");
}
} else {
if (n % 5 == 0) {
Console.WriteLine("Buzz");
} else {
Console.WriteLine(n);
}
}
}
}
using System;
class Program
{
public static void Main()
{
int n = int.Parse(Console.ReadLine());
Console.WriteLine(n%3==0?n%5==0?"FizzBuzz":"Fizz":n%5==0?"Buzz":n.ToString());
}
}
Go
3の倍数判定
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
if n % 3 == 0 {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
2つの倍数判定
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
if n % 3 == 0 && n % 5 == 0 {
fmt.Println("YES")
} else {
fmt.Println("NO")
}
}
偶奇の判定
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
even := 0
odd := 0
for i := 0; i < n; i++ {
var a int
fmt.Scan(&a)
if a % 2 == 0 {
even++
} else {
odd++
}
}
fmt.Println(even, odd)
}
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
count := [2]int{}
for i := 0; i < n; i++ {
var a int
fmt.Scan(&a)
count[a%2]++
}
fmt.Println(count[0], count[1])
}
曜日の判定
package main
import "fmt"
func main() {
var x int
fmt.Scan(&x)
switch x % 7 {
case 1:
fmt.Println("Sun")
case 2:
fmt.Println("Mon")
case 3:
fmt.Println("Tue")
case 4:
fmt.Println("Wed")
case 5:
fmt.Println("Thu")
case 6:
fmt.Println("Fri")
default:
fmt.Println("Sat")
}
}
package main
import "fmt"
var WDAY = []string{ "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" }
func main() {
var x int
fmt.Scan(&x)
fmt.Println(WDAY[x%7])
}
FizzBuzz
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
if n % 3 == 0 {
if n % 5 == 0 {
fmt.Println("FizzBuzz")
} else {
fmt.Println("Fizz")
}
} else {
if n % 5 == 0 {
fmt.Println("Buzz")
} else {
fmt.Println(n)
}
}
}
Java
3の倍数判定
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
if (n % 3 == 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(sc.nextInt() % 3 == 0 ? "YES" : "NO");
sc.close();
}
}
2つの倍数判定
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
if (n % 3 == 0 && n % 5 == 0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
System.out.println(n % 3 == 0 && n % 5 == 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 even = 0;
int odd = 0;
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
if (a % 2 == 0) {
even++;
} else {
odd++;
}
}
sc.close();
System.out.println("" + even + ' ' + odd);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[] count = new int[2];
for (int a : Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray()) {
count[a%2]++;
}
sc.close();
System.out.println("" + count[0] + ' ' + count[1]);
}
}
曜日の判定
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.close();
switch (x % 7) {
case 1:
System.out.println("Sun");
break;
case 2:
System.out.println("Mon");
break;
case 3:
System.out.println("Tue");
break;
case 4:
System.out.println("Wed");
break;
case 5:
System.out.println("Thu");
break;
case 6:
System.out.println("Fri");
break;
default:
System.out.println("Sat");
break;
}
}
}
import java.util.*;
public class Main {
static final String[] WDAY = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(WDAY[sc.nextInt()%7]);
sc.close();
}
}
FizzBuzz
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
if (n % 3 == 0) {
if (n % 5 == 0) {
System.out.println("FizzBuzz");
} else {
System.out.println("Fizz");
}
} else {
if (n % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(n);
}
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
System.out.println(n%3==0?n%5==0?"FizzBuzz":"Fizz":n%5==0?"Buzz":String.valueOf(n));
}
}
JavaScript
3の倍数判定
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
if (n % 3 === 0) {
console.log("YES");
} else {
console.log("NO");
}
console.log(Number(require("fs").readFileSync("/dev/stdin", "utf8")) % 3 ? "NO" : "YES");
2つの倍数判定
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
if (n % 3 === 0 && n % 5 === 0) {
console.log("YES");
} else {
console.log("NO");
}
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log(n % 3 || n % 5 ? "NO" : "YES");
偶奇の判定
const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const n = Number(lines[0]);
const A = lines[1].split(' ').map(Number);
let even = 0;
let odd = 0;
for (var i = 0; i < n; i++) {
if (A[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
console.log(even, odd);
const [N, A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n').map(s => s.split(' ').map(Number));
console.log(...Array(2).fill().map((_, j) => A.filter(a => a % 2 === j).length));
曜日の判定
const x = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
switch (x % 7) {
case 1:
console.log("Sun");
break;
case 2:
console.log("Mon");
break;
case 3:
console.log("Tue");
break;
case 4:
console.log("Wed");
break;
case 5:
console.log("Thu");
break;
case 6:
console.log("Fri");
break;
default:
console.log("Sat");
}
const WDAY = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
console.log(WDAY[Number(require("fs").readFileSync("/dev/stdin", "utf8"))%7]);
FizzBuzz
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
if (n % 3 === 0) {
if (n % 5 === 0) {
console.log("FizzBuzz");
} else {
console.log("Fizz");
}
} else {
if (n % 5 === 0) {
console.log("Buzz");
} else {
console.log(n);
}
}
const n = Number(require("fs").readFileSync("/dev/stdin", "utf8"));
console.log((n%3?"":"Fizz")+(n%5?"":"Buzz")||String(n));
Kotlin
3の倍数判定
fun main() {
val n = readLine()!!.toInt()
if (n % 3 == 0) {
println("YES")
} else {
println("NO")
}
}
fun main() {
println(if (readLine()!!.toInt() % 3 == 0) "YES" else "NO")
}
2つの倍数判定
fun main() {
val n = readLine()!!.toInt()
if (n % 3 == 0 && n % 5 == 0) {
println("YES")
} else {
println("NO")
}
}
fun main() {
val n = readLine()!!.toInt()
println(if (n % 3 == 0 && n % 5 == 0) "YES" else "NO")
}
偶奇の判定
fun main() {
val n = readLine()!!.toInt()
val A = readLine()!!.split(' ').map { it.toInt() }
var even = 0
var odd = 0
for (i in 0 until n) {
if (A[i] % 2 == 0) {
even++
} else {
odd++
}
}
println("" + even + ' ' + odd)
}
fun main() {
/* val n = */readLine()//!!.toInt()
val count = Array(2){ 0 }
for (a in readLine()!!.split(' ').map { it.toInt() }) {
count[a%2]++
}
println("" + count[0] + ' ' + count[1])
}
曜日の判定
fun main() {
val x = readLine()!!.toInt()
println(when(x % 7) {
1 -> "Sun"
2 -> "Mon"
3 -> "Tue"
4 -> "Wed"
5 -> "Thu"
6 -> "Fri"
else -> "Sat"
})
}
val WDAY = arrayOf("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri")
fun main() {
println(WDAY[readLine()!!.toInt()%7])
}
FizzBuzz
fun main() {
val n = readLine()!!.toInt()
if (n % 3 == 0) {
if (n % 5 == 0) {
println("FizzBuzz")
} else {
println("Fizz")
}
} else {
if (n % 5 == 0) {
println("Buzz")
} else {
println(n)
}
}
println()
}
fun main() {
val n = readLine()!!.toInt()
println(if (n % 3 == 0) if (n % 5 == 0) "FizzBuzz" else "Fizz" else if (n % 5 == 0) "Buzz" else n.toString())
}
PHP
3の倍数判定
<?php
$n = intval(fgets(STDIN));
if ($n % 3 === 0) {
echo "YES", PHP_EOL;
} else {
echo "NO", PHP_EOL;
}
?>
<?php
echo intval(fgets(STDIN)) % 3 ? "NO" : "YES", PHP_EOL;
?>
2つの倍数判定
<?php
$n = intval(fgets(STDIN));
if ($n % 3 === 0 && $n % 5 === 0) {
echo "YES", PHP_EOL;
} else {
echo "NO", PHP_EOL;
}
?>
<?php
$n = intval(fgets(STDIN));
echo $n % 3 || $n % 5 ? "NO" : "YES", PHP_EOL;
?>
偶奇の判定
<?php
$n = intval(fgets(STDIN));
$A = array_map("intval", explode(' ', fgets(STDIN)));
$even = 0;
$odd = 0;
for ($i = 0; $i < $n; $i++) {
if ($A[$i] % 2 == 0) {
$even++;
} else {
$odd++;
}
}
echo $even, ' ', $odd, PHP_EOL;
?>
<?php
$n = intval(fgets(STDIN));
$A = array_map("intval", explode(' ', fgets(STDIN)));
$even = array_filter($A, function($a) { return $a % 2 === 0; });
$odd = array_filter($A, function($a) { return $a % 2 === 1; });
echo count($even), ' ', count($odd), PHP_EOL;
?>
曜日の判定
<?php
$x = intval(fgets(STDIN));
switch ($x % 7) {
case 1:
echo "Sun", PHP_EOL;
break;
case 2:
echo "Mon", PHP_EOL;
break;
case 3:
echo "Tue", PHP_EOL;
break;
case 4:
echo "Wed", PHP_EOL;
break;
case 5:
echo "Thu", PHP_EOL;
break;
case 6:
echo "Fri", PHP_EOL;
break;
default:
echo "Sat", PHP_EOL;
}
?>
<?php
echo ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"][intval(fgets(STDIN)) % 7]
?>
FizzBuzz
<?php
$n = intval(fgets(STDIN));
if ($n % 3 === 0) {
if ($n % 5 === 0) {
echo "FizzBuzz", PHP_EOL;
} else {
echo "Fizz", PHP_EOL;
}
} else {
if ($n % 5 === 0) {
echo "Buzz", PHP_EOL;
} else {
echo $n, PHP_EOL;
}
}
?>
<?php
$n = intval(fgets(STDIN));
echo $n%3?($n%5?strval($n):"Buzz"):("Fizz".($n%5?"":"Buzz")), PHP_EOL;
?>
Perl
3の倍数判定
my $n = int(<STDIN>);
if ($n % 3 == 0) {
print "YES$/";
} else {
print "NO$/";
}
print int(<STDIN>) % 3 ? "NO" : "YES", $/;
2つの倍数判定
my $n = int(<STDIN>);
if ($n % 3 == 0 && $n % 5 == 0) {
print "YES$/";
} else {
print "NO$/";
}
my $n = int(<STDIN>);
print $n % 3 || $n % 5 ? "NO" : "YES", $/;
偶奇の判定
my $n = int(<STDIN>);
my $even = 0;
my $odd = 0;
my @A = map { int($_) } split ' ', <STDIN>;
for (my $i = 0; $i < $n; $i++) {
if ($A[$i] % 2 == 0) {
$even++;
} else {
$odd++;
}
}
print "$even $odd$/";
my $n = int(<STDIN>);
my @A = map { int($_) } split ' ', <STDIN>;
my @count = (0) x 2;
for (my $i = 0; $i < $n; $i++) {
$count[$A[$i] % 2]++;
}
print $count[0], ' ', $count[1], $/;
曜日の判定
use 5.010001;
my $x = int(<STDIN>);
given ($x % 7) {
when (1) { print "Sun$/"; }
when (2) { print "Mon$/"; }
when (3) { print "Tue$/"; }
when (4) { print "Wed$/"; }
when (5) { print "Thu$/"; }
when (6) { print "Fri$/"; }
default { print "Sat$/"; }
}
@WDAY = ("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri");
print $WDAY[int(<STDIN>) % 7], $/;
FizzBuzz
$n = int(<STDIN>);
if ($n % 3 == 0) {
if ($n % 5 == 0) {
print "FizzBuzz$/";
} else {
print "Fizz$/";
}
} else {
if ($n % 5 == 0) {
print "Buzz$/";
} else {
print "$n$/";
}
}
my $n = int(<STDIN>);
print ((($n%3?"":"Fizz").($n%5?"":"Buzz"))||$n, $/);
Python3
3の倍数判定
n = int(input())
if n % 3 == 0:
print("YES")
else:
print("NO")
print("NO" if int(input()) % 3 else "YES")
2つの倍数判定
n = int(input())
if n % 3 == 0 and n % 5 == 0:
print("YES")
else:
print("NO")
n = int(input())
print("NO" if n % 3 or n % 5 else "YES")
偶奇の判定
n = int(input())
A = list(map(int, input().split()))
even = odd = 0
for i in range(n):
if A[i] % 2 == 0:
even += 1
else:
odd += 1
print(even, odd)
n = int(input())
A = list(map(int, input().split()))
print(*[sum(a % 2 == i for a in A) for i in range(2)])
import numpy as np
n = int(input())
A = np.array(list(map(int, input().split())))
print(*[(A%2==i).sum() for i in range(2)])
曜日の判定
x = int(input())
x %= 7
if x == 1:
print("Sun")
elif x == 2:
print("Mon")
elif x == 3:
print("Tue")
elif x == 4:
print("Wed")
elif x == 5:
print("Thu")
elif x == 6:
print("Fri")
else:
print("Sat")
print(["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"][int(input()) % 7])
FizzBuzz
n = int(input())
if n % 3 == 0:
if n % 5 == 0:
print("FizzBuzz")
else:
print("Fizz")
else:
if n % 5 == 0:
print("Buzz")
else:
print(n)
n = int(input())
print(("" if n%3 else "Fizz")+("" if n%5 else "Buzz") or n)
Ruby
3の倍数判定
n = gets.to_i
if n % 3 == 0
puts "YES"
else
puts "NO"
end
puts gets.to_i % 3 == 0 ? "YES" : "NO"
2つの倍数判定
n = gets.to_i
if n % 3 == 0 && n % 5 == 0
puts "YES"
else
puts "NO"
end
n = gets.to_i
puts n % 3 == 0 && n % 5 == 0 ? "YES" : "NO"
偶奇の判定
n = gets.to_i
A = gets.split.map(&:to_i)
even = 0
odd = 0
n.times do |i|
if A[i] % 2 == 0
even += 1
else
odd += 1
end
end
print even, ' ', odd, "\n"
n = gets.to_i
A = gets.split.map(&:to_i)
puts 2.times.map{|i| A.filter{|a| a % 2 == i}.count}.join(' ')
曜日の判定
x = gets.to_i
case x % 7
when 1
puts "Sun"
when 2
puts "Mon"
when 3
puts "Tue"
when 4
puts "Wed"
when 5
puts "Thu"
when 6
puts "Fri"
else
puts "Sat"
end
WDAY = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
puts WDAY[gets.to_i % 7]
FizzBuzz
n = gets.to_i
if n % 3 == 0
if n % 5 == 0
puts "FizzBuzz"
else
puts "Fizz"
end
else
if n % 5 == 0
puts "Buzz"
else
p n
end
end
n = gets.to_i
puts n%3==0?n%5==0?"FizzBuzz":"Fizz":n%5==0?"Buzz":n.to_s
Scala
3の倍数判定
import scala.io.StdIn._
object Main extends App{
val n = readInt()
if (n % 3 == 0) {
println("YES")
} else {
println("NO")
}
}
import scala.io.StdIn._
object Main extends App{
println(if (readInt() % 3 == 0) "YES" else "NO")
}
2つの倍数判定
import scala.io.StdIn._
object Main extends App{
val n = readInt()
if (n % 3 == 0 && n % 5 == 0) {
println("YES")
} else {
println("NO")
}
}
import scala.io.StdIn._
object Main extends App{
val n = readInt()
println(if (n % 3 == 0 && n % 5 == 0) "YES" else "NO")
}
偶奇の判定
import scala.io.StdIn._
object Main extends App{
val n = readInt()
val A = readLine().split(' ').map { _.toInt }
var even = 0
var odd = 0
for (i <- 0 until n) {
if (A(i) % 2 == 0) {
even += 1
} else {
odd += 1
}
}
println("" + even + ' ' + odd)
}
import scala.io.StdIn._
object Main extends App{
val n = readInt()
val A = readLine().split(' ').map{_.toInt}
val even = A.filter(_ % 2 == 0)
val odd = A.filter(_ % 2 == 1)
println("" + even.length + ' ' + odd.length)
}
曜日の判定
import scala.io.StdIn._
object Main extends App{
val x = readInt()
println(x % 7 match {
case 1 => "Sun"
case 2 => "Mon"
case 3 => "Tue"
case 4 => "Wed"
case 5 => "Thu"
case 6 => "Fri"
case _ => "Sat"
})
}
import scala.io.StdIn._
object Main extends App{
val WDAY = Array("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri")
println(WDAY(readInt() % 7))
}
FizzBuzz
import scala.io.StdIn._
object Main extends App{
val n = readInt()
if (n % 3 == 0) {
if (n % 5 == 0) {
println("FizzBuzz")
} else {
println("Fizz")
}
} else {
if (n % 5 == 0) {
println("Buzz")
} else {
println(n)
}
}
}
import scala.io.StdIn._
object Main extends App{
val n = readInt()
println(if (n % 3 == 0) if (n % 5 == 0) "FizzBuzz" else "Fizz" else if (n % 5 == 0) "Buzz" else n.toString)
}
Swift
3の倍数判定
let n = Int(readLine()!)!
if n % 3 == 0 {
print("YES")
} else {
print("NO")
}
print(Int(readLine()!)! % 3 == 0 ? "YES" : "NO")
2つの倍数判定
let n = Int(readLine()!)!
if n % 3 == 0 && n % 5 == 0 {
print("YES")
} else {
print("NO")
}
let n = Int(readLine()!)!
print(n % 3 == 0 && n % 5 == 0 ? "YES" : "NO")
偶奇の判定
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
var even = 0
var odd = 0
for i in 0..<n {
if A[i] % 2 == 0 {
even += 1
} else {
odd += 1
}
}
print(even, odd)
let n = Int(readLine()!)!
let A = readLine()!.split(separator: " ").compactMap { Int($0) }
let even = A.filter { $0 % 2 == 0 }
let odd = A.filter { $0 % 2 == 1 }
print(even.count, odd.count)
曜日の判定
let x = Int(readLine()!)!
switch x % 7 {
case 1:
print("Sun")
case 2:
print("Mon")
case 3:
print("Tue")
case 4:
print("Wed")
case 5:
print("Thu")
case 6:
print("Fri")
default:
print("Sat")
}
let WDAY = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]
print(WDAY[Int(readLine()!)!%7])
FizzBuzz
let n = Int(readLine()!)!
if n % 3 == 0 {
if n % 5 == 0 {
print("FizzBuzz")
} else {
print("Fizz")
}
} else {
if n % 5 == 0 {
print("Buzz")
} else {
print(n)
}
}