解题思路
排列:4P3
mathu.h
//
// Created by crazy_rays on 2022/8/30.
//
/**
* 计算阶乘
* @param num 要计算阶乘的数字
* @return 阶乘结果
*/
int factorial(int num);
/**
* 排列:n!/(n-m)!
* @param n 总数
* @param m 排列数
* @return 排列结果数
*/
int permutation(int n, int m);
/**
* 组合:n!/m!(n-m)!
* @param n 总数
* @param m 组合数
* @return 组合结果数
*/
int combination(int n, int m);
mathu.c
//
// Created by crazy_rays on 2022/8/30.
//
int factorial(int num) {
return (num == 0) ? 1 : num * factorial(num - 1);
}
int permutation(int n, int m) {
return factorial(n) / factorial(n - m);
}
int combination(int n, int m) {
return factorial(n) / (factorial(m) * factorial(n - m));
}
L1.h
//
// Created by crazy_rays on 2022/8/30.
// References from : https://www.runoob.com/cprogramming/c-100-examples.html
//
#ifndef CTEST100_L1_H
#define CTEST100_L1_H
#endif //CTEST100_L2_H
#include <stdio.h>
#include "../include/mathu.h"
/**
* 题目:有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
* 程序分析:可填在百位、十位、个位的数字都是 1、2、3、4,组成所有的排列后再去掉不满足条件的排列。
*/
void L1();
L1.c
//
// Created by crazy_rays on 2022/8/30.
// References from : https://www.runoob.com/cprogramming/c-100-examples.html
//
#include "../include/L1.h"
/**
* 题目:有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
* 程序分析:可填在百位、十位、个位的数字都是 1、2、3、4,组成所有的排列后再去掉不满足条件的排列。
*/
void L1() {
printf("\nLesson 1 :\n");
//4P3排列:n!/(n-m)! = 4!/(4-3)!
printf("有%d种方式\n", factorial(4));
// 百十个三层循环
for (int hundred = 1; hundred <= 4; ++hundred) {
for (int ten = 1; ten <= 4; ++ten) {
for (int one = 1; one <= 4; ++one) {
// 判断排列结果中是否有同一个数字
if (ten == hundred || one == ten || one == hundred) {
continue;
}
printf("%d%d%d\n", hundred, ten, one);
}
}
}
}
main.c
#include "include/L1.h"
int main() {
L1();
return 0;
}