|
#include <stdio.h>
int main() {
const int DAYS = 7; // 一周天数
const int GENDER = 2; // 性别数(女/男)
const int SPECIFIC_DAY = 0; // 目标出生日(0=星期一)
// 计算总状态数
const int STATES_PER_CHILD = DAYS * GENDER; // 14种状态/孩子
const int TOTAL_STATES = STATES_PER_CHILD * STATES_PER_CHILD; // 196种组合
// 计算关键数值(使用组合数学)
// 1. 至少有一个周一男孩的组合数
int at_least_one_monday_boy =
STATES_PER_CHILD // 第一个孩子是周一男孩(第二个任意)
+ (STATES_PER_CHILD - 1) // 第二个孩子是周一男孩(第一个非周一男孩)
;
// 2. 两个都是男孩的组合数
int both_boys = DAYS * DAYS; // 7×7=49
// 3. 两个都是男孩且至少有一个周一男孩
int boys_with_monday =
both_boys // 所有男孩组合
- (DAYS - 1) * (DAYS - 1); // 减去没有周一男孩的组合(6×6=36)
// 输出结果
printf("总组合数: %d\n", TOTAL_STATES);
printf("至少有一个周一男孩的组合数: %d\n", at_least_one_monday_boy);
printf("两个都是男孩且至少有一个周一男孩: %d\n", boys_with_monday);
printf("概率 = %d / %d = %.6f\n",
boys_with_monday,
at_least_one_monday_boy,
(double)boys_with_monday / at_least_one_monday_boy);
return 0;
}
不知我这样能不能,避免嵌套循环。 |
|