鱼C论坛

 找回密码
 立即注册
查看: 2890|回复: 2

[已解决]排列组合问题

[复制链接]
发表于 2023-1-19 20:10:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目
输入:
全由小写字母组成的单词,如:dad
输出:
由输入单词的字母构成的不同排列方式。如对于dad,有:add, dad, dda,3种不同的排列方式
样例:
输入:dad
输出:3

我的代码:
import math
str = input()
c = len(str)
p =math.comb(c,1)
print(p)


这个代码可以计算样例,但是我输入peoper这个,代码运行出来的是6,正确答案是60,这是为什么,求解答
最佳答案
2023-1-19 20:29:17
本帖最后由 isdkz 于 2023-1-19 20:50 编辑

第一、math.comb(c,1) 得到的是 Cc1的结果,C61的值可不就是6吗

第二、你这个题目要求的是排列方式,不是组合,

第三、你确定正确答案是 60,而不是 180?

示例代码一:
from itertools import permutations
string = input()
print(len(set(permutations(string))))

示例代码二:
from math import perm
from collections import Counter
string = input()
length = len(string)
c = Counter(string)
result = perm(length, length)
for i in c.values():
    if i != 1:
        result //= perm(i, i)
print(result)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-19 20:20:38 | 显示全部楼层
math.comb(c, c)
看看吧。
这题用现成的函数来解意义不大。再说了函数要3.8才能运行的了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-19 20:29:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-1-19 20:50 编辑

第一、math.comb(c,1) 得到的是 Cc1的结果,C61的值可不就是6吗

第二、你这个题目要求的是排列方式,不是组合,

第三、你确定正确答案是 60,而不是 180?

示例代码一:
from itertools import permutations
string = input()
print(len(set(permutations(string))))

示例代码二:
from math import perm
from collections import Counter
string = input()
length = len(string)
c = Counter(string)
result = perm(length, length)
for i in c.values():
    if i != 1:
        result //= perm(i, i)
print(result)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-24 21:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表