สมมติว่ามีตัวเลข 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