{ }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()·표준 입력으로 들어가요(실행 중에 묻지 않아요).

$ 준비됐어요

코드를 입력해 풀려면 PC에서 열어 주세요. 모바일에서는 문제와 풀이를 볼 수 있어요.

다른 언어로는?언어별 풀이 보기 · 8개 언어 풀이

비교의 결과는 참거짓 값(불값)이고, 변수에 담아 두었다가 &&로 다시 묶을 수 있어요(Python은 and).
그런데 불값을 그대로 찍으면 언어마다 다르게 나와요.
Java·JS·TS·크레이콘은 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을 뺀 일곱 언어(크레이콘 포함)는 -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);