{ }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);