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

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

# hundreds_digit、tens_digit、units_digit分别表示这个三位数的百位、十位、个位
# 打印三位数:使用三重循环,当这个三位数的百位、个位、个位互不相等时,将这个三位数打印出来
# 计数:将符合条件的三位数依次加入到列表中,使用len函数计算出列表元素个数
 
three_digit_list = []
for hundreds_digit in range(1,5):
    for tens_digit in range(1,5):
        for units_digit in range(1,5):
            if hundreds_digit != tens_digit and hundreds_digit != units_digit and tens_digit != units_digit:
                print(f"{hundreds_digit}{tens_digit}{units_digit}")
                three_digit = str(hundreds_digit)+str(tens_digit)+str(units_digit)
                three_digit_list.append(three_digit)
print(f"一共有{len(three_digit_list)}个符合条件的三位数")

  

 

counter = 0
for i in range(1,5):
    for j in range(1,5):
        if i == j : continue
        else:
            for k in range(1,5):
                if j == k or i == k :continue
                else:
                    print(f"{i}{j}{k}")
                    counter += 1
print(f"一共有{counter}个符合条件的三位数")

  

posted @ 2023-05-16 11:09  sangern  阅读(155)  评论(0)    收藏  举报