Official

A - Digits of A+B Editorial by sheyasutaka Editorial for printing


初心者の方へ

解法 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: