【每日一题】有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

本文介绍了一个具体的组合数学问题,即使用数字1、2、3、4可以构成多少个不同的无重复数字的三位数,并提供了两种解决方法:一种是利用Python的排列组合函数permutations;另一种是采用多层循环的方法来实现。

题目:1.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

思路一:利用排列组合函数

  1. 通过排列组合permutations,首先得到所有互不相同且无重复数字的组合,得到列表元组;

from itertools import permutations

num = [1, 2, 3, 4]
result = []
#得到num的所有三个数字的排列组合
for i in permutations(num,3):
    result.append(i)
print(result)#得到所有三个不重复数字的排列组合
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
  1. 列表推导式获取列表中的每个元组元素;

r1 = result[1] #取出列表中的第一个元素(该元素为元组:(1, 2, 4))
  1. str将元组中的元素转换为字符串,join将每个元组中的字符串元素连接起来,得到要求的三位数;

''.join(str(i) for i in r1) #将该元组所有元素逐个转换为字符串,并通过join连接起来,得到三位数字符串'124'
int(''.join(str(i) for i in result[1])) #将得到的三位字符串转换为整数

实现过程:

from itertools import permutations

num = [1, 2, 3, 4]
result = []
#得到num的所有三个数字的排列组合
for i in permutations(num,3):
    result.append(i)

#将每个元组元素转换为字符串并连接起来为一个三位数
strResult = []
for j in range(len(result)):
    # print(result[j])
    strResult.append(int(''.join(str(i) for i in result[j])))
print(strResult)

结果:

[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
print(len(strResult))
24

思路二:通过多层循环

number = [1, 2, 3, 4]
count = 0
result=[]

for i in number:
    for j in number:
        for k in number:
            if i !=j and j !=k and k !=i:
                count += 1
                # print(i,j,k)
                result.append((i,j,k))

print(result)
print(count)
#输出
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
24
可以使用多种编程语言来计算列表 `[1, 2, 3, 4]` 能组成互不相同无重复数字三位数的数量。 ### Java 实现 ```java package com.test.demo; public class Test6 { public static void main(String[] args) { // 符合条件的三位数计数器 int count = 0; // 百位数,由1~4组成 for (int i = 1; i <= 4; i++) { // 十位数,由1~4组成 for (int j = 1; j <= 4; j++) { // 个位数,由1~4组成 for (int k = 1; k <= 4; k++) { // 判等,确保 百位数 ≠ 十位数,十位数 ≠ 个位数,百位数 ≠ 个位数 if (i != j && j != k && i != k) { count++; } } } } System.out.println("1、2、3、4这4个数字组成 " + count + " 个互不相同无重复数字三位数"); } } ``` ### Python 实现(三重循环) ```python arrange = 0 nums = range(1, 5) for i in nums: for j in nums: for k in nums: if ((i != j) and (j != k) and (k != i)): arrange += 1 print(arrange) ``` ### Python 实现(非三重循环) ```python count = 0 for num in range(100, 450): num_str = str(num) digits = [int(d) for d in num_str] if all(digit in [1, 2, 3, 4] for digit in digits) and len(set(digits)) == 3: count += 1 print(count) ``` ### C 语言实现 ```c #include <stdio.h> int main() { int num = 0; int i = 0, j = 0, k = 0; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { if (i == j) { continue; } for (k = 1; k <= 4; k++) { if (j == k || i == k) { continue; } num++; } } } printf("%d", num); return 0; } ``` 通过以上代码都可以计算出列表 `[1, 2, 3, 4]` 能组成互不相同无重复数字三位数的数量为 24 个 [^1][^2][^3][^4][^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值