Official
A - Thermometer Editorial
by
A - Thermometer Editorial
by
MtSaka
初心者の方へ
- プログラミングの学習を始めたばかりで何から手をつけるべきかわからない方は、まずは practice contest の問題A「Welcome to AtCoder」をお試しください。言語ごとに解答例が掲載されています。
- また、プログラミングコンテストの問題に慣れていない方は、 AtCoder Beginners Selection の問題をいくつか試すことをおすすめします。
- C++入門 AtCoder Programming Guide for beginners (APG4b) は、競技プログラミングのための C++ 入門用コンテンツです。
- Python入門 AtCoder Programming Guide for beginners (APG4bPython) は、競技プログラミングのための Python 入門用コンテンツです。
この問題は小数の入力、if文を用いることが求められている問題です。
小数の入力はC++の場合は double、Pythonの場合は float を入力として受け取る各言語の標準的な方法でできます。
入力で受け取った値 \(X\) について、
- \(38.0\) \({}^\circ\)C 以上か
- \(37.5\) \({}^\circ\)C 以上 \(38.0\) \({}^\circ\)C 未満か
- \(37.5\) \({}^\circ\)C 未満か
をif文で判定し、対応する整数を出力することで正解できます。
また、別解として小数として入力を受け取るのではなく文字列として受け取り、整数部分と小数部分にそれぞれ分割してif文で判定する解法もあります。
実装例(C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
double t;
cin >> t;
if (38.0 <= t) {
cout << 1 << endl;
}
else if (37.5 <= t && t < 38.0) {
cout << 2 << endl;
}
else {
cout << 3 << endl;
}
}
実装例(Python)
t = float(input())
if 38.0 <= t:
print(1)
elif 37.5 <= t < 38.0:
print(2)
else:
print(3)
実装例(別解 C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int a = stoi(s.substr(0, 2));
int b = stoi(s.substr(3, 1));
if (38 <= a) {
cout << 1 << endl;
} else if (a == 37 && 5 <= b) {
cout << 2 << endl;
} else {
cout << 3 << endl;
}
}
posted:
last update:
