{ }codelper
真偽値P0217

真と偽を四行

一行に整数 a と b が空白一つで区切って与えられる。
次の四つの問いに順に一行ずつ答えよ。
① a は b より小さいか?
② a と b は等しいか?
③ a は奇数か?
(負の数も 2 で割り切れなければ奇数である。
-3 は奇数で、0 は偶数である。) ④ ① と ③ がどちらも真か?
真なら小文字で true、偽なら小文字で false を出力する。

入力
-1,000 ≤ a, b ≤ 1,000
出力
四行、各行は true か false
例 1 · stdin
3 5
stdout
true
false
true
true
例 2 · stdin
-3 -3
stdout
false
true
true
false
main.py
入力(stdin)

実行すると、これらの行が順に input()・標準入力に渡されます(実行中に入力を求めません)。

$ 準備完了

コードを入力して解くにはパソコンで開いてください。モバイルでは問題と解答を見られます。

ほかの言語では?言語別の解答を見る · 8 言語の解答

比較の結果は真偽値(ブール値)で、変数に入れておいて && でまた組み合わせられます(Python は and)。
ところがブール値をそのまま出力すると言語ごとに違います。
Java・JS・TS・Craykon は true / false、Python print(x) と C# Console.WriteLine(x) は True / False(大文字始まり)、C++ std::cout << x は 1 / 0(std::boolalpha を入れると true / false)、C は bool が整数なので printf("%d") で 1 / 0 です。
そこで Python・C#・C は "true" if x else "false"、x ? "true" : "false" のように単語に変えて出力します。
二つ目の落とし穴は奇数の判定です。
Python 以外の七つの言語(Craykon を含む)では -3 % 2 が -1 なので、a % 2 == 1 で聞くと負の奇数が偽になります。
a % 2 != 0 で聞けばどの言語でも正しくなります(Python は -3 % 2 が 1 なのでどちらでも大丈夫です)。

Pythonmain.py · 8 行
a, b = map(int, input().split())less = a < bsame = a == bodd = a % 2 != 0   # 음수 홀수도 0이 아니다both = less and odd# print(True)는 True를 찍는다. 소문자로 바꿔 쓴다for v in (less, same, odd, both):    print("true" if v else "false")
JavaScriptmain.js · 7 行
const [a, b] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const less = a < b;const same = a === b;const odd = a % 2 !== 0; // -3 % 2는 -1이라 === 1로 물으면 안 된다const both = less && odd;// 불값은 문자열로 바뀌면 "true" / "false"다console.log([less, same, odd, both].join("\n"));
TypeScriptmain.ts · 7 行
import { readFileSync } from "node:fs";const [a, b]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);const less: boolean = a < b;const same: boolean = a === b;const odd: boolean = a % 2 !== 0;const both: boolean = less && odd;console.log([less, same, odd, both].join("\n"));
JavaMain.java · 18 行
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();        boolean less = a < b;        boolean same = a == b;        boolean odd = a % 2 != 0; // -3 % 2 == -1        boolean both = less && odd;        // Java는 boolean을 true / false로 찍는다        System.out.println(less);        System.out.println(same);        System.out.println(odd);        System.out.println(both);    }}
C#Main.cs · 22 行
using System; class Program{    static string Word(bool v) => v ? "true" : "false";     static void Main()    {        string[] p = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);        int a = int.Parse(p[0]);        int b = int.Parse(p[1]);        bool less = a < b;        bool same = a == b;        bool odd = a % 2 != 0;        bool both = less && odd;        // Console.WriteLine(true)는 True(대문자)를 찍는다        Console.WriteLine(Word(less));        Console.WriteLine(Word(same));        Console.WriteLine(Word(odd));        Console.WriteLine(Word(both));    }}
C++main.cpp · 12 行
#include <iostream> int main() {    int a, b;    std::cin >> a >> b;    bool less = a < b;    bool same = a == b;    bool odd = a % 2 != 0;    bool both = less && odd;    // std::boolalpha가 없으면 1과 0으로 찍힌다    std::cout << std::boolalpha << less << "\n" << same << "\n" << odd << "\n" << both << "\n";}
Cmain.c · 16 行
#include <stdbool.h>#include <stdio.h> static const char *word(bool v) { return v ? "true" : "false"; } int main(void) {    int a, b;    scanf("%d %d", &a, &b);    bool less = a < b;    bool same = a == b;    bool odd = a % 2 != 0; /* -3 % 2 == -1 */    bool both = less && odd;    /* C의 bool은 printf로 찍으면 1과 0이다 */    printf("%s\n%s\n%s\n%s\n", word(less), word(same), word(odd), word(both));    return 0;}
Craykonmain.cray · 12 行
p = コンソール.行読み().空白除去().分ける(" ");a = p[0].数に();b = p[1].数に();less = a < b;same = a == b;odd = a % 2 != 0; // -3 % 2는 -1이다both = less && odd;// 크레이콘은 참거짓을 true / false로 쓴다コンソール.書く(less);コンソール.書く(same);コンソール.書く(odd);コンソール.書く(both);