公式

A - What month is it? 解説 by sounansya


AtCoder をはじめたばかりで何をしたらよいか分からない方は、まずは practice contest の問題A「Welcome to AtCoder」を解いてみてください。基本的な入出力の方法が載っています。
また、プログラミングコンテストの問題に慣れていない方は、AtCoder Beginners Selection の問題をいくつか解いてみることをおすすめします。


入力例 \(2\)\(X=1,Y=1\) の場合のように \(X\)\(Y\) の両方が小さい場合の答えは \(X+Y\) となります。しかし、入力例 \(1\) のように \(X+Y\)\(12\) より大きい場合は \(X+Y\) から \(12\) を引いた \(X+Y-12\) が答えとなります。

以上を適切に実装することでこの問題に正答することができます。

実装例(Python3)

x, y = map(int, input().split())
if x + y <= 12:
    print(x + y)
else:
    print(x + y - 12)

実装例(C++)

#include <bits/stdc++.h>
using namespace std;
int main() {
	int x, y;
	cin >> x >> y;
	if (x + y <= 12) {
		cout << x + y << endl;
	} else {
		cout << x + y - 12 << endl;
	}
	return 0;
}

実装例(C)

#include <stdio.h>
int main() {
	int x, y;
	scanf("%d %d", &x, &y);
	if (x + y <= 12) {
		printf("%d\n", x + y);
	} else {
		printf("%d\n", x + y - 12);
	}
	return 0;
}

実装例(Java)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        if (x + y <= 12) {
            System.out.println(x + y);
        } else {
            System.out.println(x + y - 12);
        }
        sc.close();
    }
}

投稿日時:
最終更新: