-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Permutation

Luz3年前 (2022-05-10)题库800
In mathematics, a permutation of a set is, loosely speaking, an arrangement of its members into a sequence or linear order, or if the set is already ordered, a rearrangement of its elements. The word "permutation" also refers to the act or process of changing the linear order of an ordered set.

The simplest example of permutations is permutations without repetitions where we consider the number of possible ways of arranging $n$ items into $n$ places.

In a similar manner, the number of arrangements of $r$ items from $n$ objects is considered a partial permutation. It is written as ${\displaystyle P^n_r}$ (which reads "n permute r"), and is equal to the number ${\displaystyle n(n-1)\cdots (n-r+1)}$ (also written as $\displaystyle \frac {n!}{(n-r)!}$ ).

The following program reads two numbers $n$ and $r$ in, and prints out the value of $\displaystyle P^n_r$.

java
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int r = in.nextInt();
int res = IntStream.range(, ).
reduce(, (acc, k)->);
System.out.println(res);
in.close();
}
}


FYR: In Java Doc, the reduce function of Stream is described as:
java
T reduce(T identity, BinaryOperator<T> accumulator)


It performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.






答案:
第1空:n-r+1

第2空:n+1

第3空:1

第4空:acc*k

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。