gpt4 book ai didi

perl - 在 Perl 中打印出独特的行

转载 作者:行者123 更新时间:2023-12-02 07:45:39 25 4
gpt4 key购买 nike

我遇到一道数学题。给定十个数字(在本例中为从 1 到 10 的数字),六个数字的独特组合有多少种?简短的回答是 210。但是,我想知道这些组合是什么。

我将以下代码放在一起。第一个 while 循环可以很好地创建很多排序组合,但是,我无法只打印出唯一的行组合。我怎样才能打印出这些独特的线条?

my %hash;
my @sorted_numbers;

# make all permutations with numbers from 1 to 10
my $permutor = List::Permutor->new (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
while ( my @permutation = $permutor->next() ) {
my $string = "@permutation";
my ($N1, $N2, $N3, $N4, $N5, $N6, $N7, $N8, $N9, $N10) = split (/ /, $string);

# chose only the first six numbers and sort them in ascending order
my @numbers = ($N1, $N2, $N3, $N4, $N5, $N6);
@sorted_numbers = sort {$a <=> $b} @numbers;
}

# print out the unique number combinations from the temp file
my @unique_combinations = uniq @sorted_numbers;
foreach ( @unique_combinations ) {
print $_, "\n";
}

最佳答案

可能还有其他的 CPAN 模块,但这是一种方法:

use Math::Combinatorics qw(combine);
my @comb = combine(6, 1..10);

关于perl - 在 Perl 中打印出独特的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7238219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com