paizaラーニングのレベルアップ問題集「条件分岐メニュー」からの出典です。
paiza.jp
Javaによる「積の最小化」問題集と、それらの提出コードの解答例です。
僕が作成、提出したコードは、以下のとおりです。
・STEP: 1 けた数の測定
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 1 けた数の測定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println(str.length()); } }
・STEP: 2 足したり引いたり
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 2 足したり引いたり https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a = sc.nextInt(); int b = sc.nextInt(); int ans1 = n + a + b; int ans2 = n + a - b; int ans3 = n - a + b; int ans4 = n - a - b; if(ans1 == 0 || ans2 == 0 || ans3 == 0 || ans4 == 0){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
・STEP: 3 同値判定
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 3 同値判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; import java.util.ArrayList; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); ArrayList lis1 = new ArrayList(); ArrayList lis2 = new ArrayList(); for(int i = 0; i < n; i++){ int num = sc.nextInt(); lis1.add(num); } for(int i = 0; i < n; i++){ int num = sc.nextInt(); lis2.add(num); } int count = 0; for(int i = 0; i < n; i++){ if(lis1.get(i) == lis2.get(i)){ count++; } } System.out.println(count); } }
・STEP: 4 終了判定
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 4 終了判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 0; for(int i = 0; i < n; i++){ int num = sc.nextInt(); if(num%2 == 0){ count = count + num; }else{ break; } } System.out.println(count); } }
・STEP: 5 終了判定 2
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 5 終了判定 2 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); int m = 0; while(n < k){ n = n*2; m++; } System.out.println(m); } }
・STEP: 6 池の周回
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 6 池の周回 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; 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(); int num = 0; for(int i = 0; i < t; i++){ num = num + k; } if(num % n == 0){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
・STEP: 7 崖に落ちるか判定
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 7 崖に落ちるか判定 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; 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(); int num = k*n; if(num <= t){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
・STEP: 8 タイルの敷き詰め
/* Javaによる「条件分岐メニュー」問題集(積の最小化) STEP: 8 タイルの敷き詰め https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int h = sc.nextInt(); int w = sc.nextInt(); if(h == 0 || w == 0){ System.out.println("NO"); }else if(h%2 == 0 && w%2 == 0){ System.out.println("YES"); }else{ System.out.println("NO"); } } }
・FINAL問題: 積の最小化
/* Javaによる「条件分岐メニュー」問題集(積の最小化) FINAL問題: 積の最小化 https://paiza.jp/works/mondai 提出コードの解答例 https://neguse-atama.hatenablog.com */ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); if(a < 0 && b < 0){ if(a < b){ System.out.println(b*b); }else{ System.out.println(a*a); } }else if(a > 0 && b > 0){ if(a < b){ System.out.println(a*a); }else{ System.out.println(b*b); } }else{ System.out.println(a*b); } } }
paizaラーニングのレベルアップ問題集については、ユーザー同士で解答を教え合ったり、コードを公開したりするのは自由としています。
また授業や研修、教材などにも利用できるそうです。