当前位置:首页 > 实验代码 > 正文内容

python数字组合

千帆2年前 (2024-12-24)实验代码8317

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

遍历全部可能,把有重复的替换掉。


num=0

for a in range(1,5):

    for b in range(1,5):

        for c in range(1,5):

            if((a!=b)and(a!=c)and(b!=c)):

              print(a,b,c)

              num+=1

print (num)

扫描二维码推送至手机访问。

版权声明:本文由千帆生活网发布,如需转载请注明出处。

本文链接:http://ntshw.com/?id=185

分享给朋友:

“python数字组合” 的相关文章

Python计算每个月天数

import calendar'''输出的是一个元组,第一个元素是所查月份的第一天对应的是星期几(0-6),第二个元素是这个月的天数。以上实例输出的意思为 2016 年 9 月份的第一天是星期四,该月总共有 30 天。'''monthRange = c...

页面完全居中的css代码

在前台页面制作时,可能会遇到需要完全居中的效果,可以使用以下代码,简单实用。.center-box {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%...

python读取记事本并输出

# 假设记事本文件名为2.txt,位于C盘filename = 'c:/2.txt'# 使用with语句确保文件正确关闭with open(filename, 'r', encoding='utf-8') as file:    c...

python文本处理:统计文本中单词出现的频率

text = "This is a sample text for word frequency analysis."words = text.split()word_count = {}for word in words:    if word in wor...

python文件操作:复制文件

import shutilsrc_file = 'source.txt'dest_file = 'destination.txt'shutil.copyfile(src_file, dest_file)...

python图像处理:调整图像大小

from PIL import Imageimage = Image.open('1.png')resized_image = image.resize((800, 600))resized_image.save('resized_image.png')...