GESP2024年12月认证C++八级( 第一部分选择题(6-10))
选择题7
#include <iostream>
using namespace std;// 计算阶乘函数(递归 or 循环都可)
long long factorial(int n) {long long result = 1;for (int i = 2; i <= n; ++i)result *= i;return result;
}// 计算组合数 C(n, k)
long long combination(int n, int k) {if (k < 0 || k > n) return 0;return factorial(n) / (factorial(k) * factorial(n - k));
}int main() {int a, b;cout << "请输入项的指数 a 和 b(对应 x^a y^b):";cin >> a >> b;int n = a + b; // 总次数 nlong long coeff = combination(n, b); // 或者 combination(n, a)cout << "在 (x + y)^" << n << " 的展开式中,x^" << a << "y^" << b << " 的系数是:" << coeff << endl;return 0;
}
选择题10
#include <iostream>
using namespace std;int main() {int N = 15, cnt = 0;for (int x = 0; x + x + x <= N; x++)for (int y = x; x + y + y <= N; y++)for (int z = y; x + y + z <= N; z++)cnt++;cout << cnt << endl;return 0;
}