整数の「各桁の和」を求める問題
問題概要
1 以上
考えたこと
まず、整数
各桁の和を求める部分を関数化すると考えやすくなるので、これからは整数 n の各桁の和を求める関数を calc(int n) などとする。
そうすると、calc(n) が
コード
#include <bits/stdc++.h> using namespace std; int calc(int n) { int res = 0; while (n > 0) { res += n % 10; n /= 10; } return res; } int main() { int N, A, B; cin >> N >> A >> B; int res = 0; for (int n = 1; n <= N; n++) { int wa = calc(n); if (wa >= A && wa <= B) res += n; } cout << res << endl; }