สมมติว่าเรามีตัวเลข n เราต้องหาหลายวิธีในการหารตัวเลขออกเป็นส่วน ๆ (a, b, c และ d) ที่ a =c และ b =d ดังนั้นหากตัวเลขคือ 20 ผลลัพธ์จะเป็น 4 เช่น [1, 1, 9, 9], [2, 2, 8, 8], [3, 3, 7, 7] และ [4, 4, 6 , 6]
ดังนั้นหาก N เป็นคี่ คำตอบจะเป็น 0 หากตัวเลขหารด้วย 4 ลงตัว คำตอบจะเป็น n/4 – 1 มิฉะนั้น n/4
ตัวอย่าง
#include <iostream>
using namespace std;
int countPossiblity(int num) {
if (num % 2 == 1)
return 0;
else if (num % 4 == 0)
return num / 4 - 1;
else
return num / 4;
}
int main() {
int n = 20;
cout << "Number of possibilities: " << countPossiblity(n);
} ผลลัพธ์
Number of possibilities: 4