C++ 统计数字2出现的次数 - 算法题解
#include
int count2(int num) { int count = 0; while (num > 0) { if (num % 10 == 2) { count++; } num /= 10; } return count; }
int main() { int L, R; cin >> L >> R; int sum = 0; for (int i = L; i <= R; i++) { sum += count2(i); } cout << sum << endl; return 0; }
原文地址: https://www.cveoy.top/t/topic/o7Ry 著作权归作者所有。请勿转载和采集!