Cho mảng holes[10] số lỗ của chữ số 0-9. Tính tổng lỗ của số nguyên nhập vào.
Ví dụ
holes[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1} (tham khảo: 0=1, 4=1, 6=1, 8=2, 9=1)
Số 239143: 2(0)+3(0)+9(1)+1(0)+4(1)+3(0) = 2
Ví dụ hoàn chỉnh
#include <bits/stdc++.h>
using namespace std;
int holes_integer(int number, int holes[]) {
int count = 0;
while (number > 0) {
int digit = number % 10;
count += holes[digit];
number /= 10;
}
return count;
}
int main() {
int number = 239143;
int holes[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
cout << "Số lỗ: " << holes_integer(number, holes);
return 0;
}
Kết quả
Số lỗ: 2