用 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

更新日期:19-Dec-2019

167 次瀏覽

開始你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.