用 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