Official
A - Digits of A+B Editorial
by
A - Digits of A+B Editorial
by
sheyasutaka
Editorial for printing
初心者の方へ
- プログラミングの学習を始めたばかりで何から手をつけるべきかわからない方は、まずは 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 入門用コンテンツです。
解法 1
\(A+B\) の値を求めて文字列に直し,その長さを得ればよいです.
解法 2
\(1 \leq A, B \leq 9\) という制約下において,\(A+B \geq 10\) ならば答えは \(2\),\(A+B < 10\) ならば答えは \(1\) です.
実装例 (C++)
解法 1
#include <iostream>
using std::cin;
using std::cout;
#include <string>
using std::string;
using std::to_string;
int main (void) {
int a, b;
cin >> a >> b;
int ans = to_string(a + b).size();
cout << ans << "\n";
return 0;
}
解法 2
#include <iostream>
using std::cin;
using std::cout;
int main (void) {
int a, b;
cin >> a >> b;
int ans;
if (a + b >= 10) {
ans = 2;
} else {
ans = 1;
}
cout << ans << "\n";
return 0;
}
posted:
last update:
