โจทย์: ให้ตัวเลขจำนวนเต็ม ต้องการนับจำนวน "รู" (holes) ในตัวเลข โดยนิยาม:
- 0: 1 รู (หรือ 2 รูในบางฟอนต์)
- 1: 0 รู
- 2: 0 รู
- 3: 0 รู
- 4: 1 รู (หรือ 0)
- 5: 0 รู
- 6: 1 รู
- 7: 0 รู
- 8: 2 รู
- 9: 1 รู
หมายเหตุ: โค้ดต้นฉบับใช้ array `holes[] = {2,1,1,0,0,1,1,1,0}` ซึ่งมีค่าไม่ตรงกับมาตรฐานทั่วไป (0 มี 2 รู, 1 มี 1 รู, 2 มี 1 รู...)
โค้ด C++ (ใช้ mapping ตามโจทย์)
#include <bits/stdc++.h>
using namespace std;
int count_holes(int number, const int holes[]) {
int count = 0;
if (number == 0) return holes[0]; // กรณีพิเศษ: 0
while (number > 0) {
int digit = number % 10;
count += holes[digit];
number /= 10;
}
return count;
}
int main() {
// Mapping ตามโค้ดต้นฉบับ (อาจแตกต่างจากมาตรฐาน)
int holes[] = {2, 1, 1, 0, 0, 1, 1, 1, 0, 0}; // เพิ่ม index 9
int num1 = 239143;
cout << "Number: " << num1 << " -> Holes: " << count_holes(num1, holes) << endl;
int num2 = 12345;
cout << "Number: " << num2 << " -> Holes: " << count_holes(num2, holes) << endl;
// Mapping มาตรฐานทั่วไป
int standard_holes[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
cout << "Standard mapping for 239143: " << count_holes(239143, standard_holes) << endl;
return 0;
}
ผลลัพธ์ (ตาม mapping โจทย์)
Number: 239143 -> Holes: 2 Number: 12345 -> Holes: 3 Standard mapping for 239143: 3
คำอธิบาย
239143 ตาม mapping โจทย์: 2(1) + 3(0) + 9(0) + 1(1) + 4(0) + 3(0) = 2
12345: 1(1) + 2(1) + 3(0) + 4(0) + 5(1) = 3
ข้อควรระวัง: Mapping ของ "รู" ขึ้นอยู่กับฟอนต์/รูปแบบการเขียน ควรกำหนดชัดเจนก่อนใช้งาน