用 C++ 查詢將數字分成四部分的方法,使 a = c 和 b = d
假設我們有一個數字 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP