打印给定数字的排列 (nPr) 的程序

2025年1月8日 | 阅读需时 2 分钟

排列

它是事物集合或对象集合的一个有序排列/组合。

例如,我们有一个字母集合 A、B 和 C……描述了从 r 个不同对象中取 n 个的排列。

n 个元素的列表的排列

算法

  • 步骤 1: 开始
  • 步骤 2: 定义 n、r、per、fact1、fact2
  • 步骤 3: 打印 n、r
  • 步骤 4: fact1 = n
  • 步骤 5: 重复步骤 6 直到 i >= 1
  • 步骤 6: fact1 = fact1 * i
  • 步骤 7: 定义 number
  • 步骤 8: 设置 number = n - r
  • 步骤 9: fact2 = fact2 * i
  • 步骤 10: 设置 per = fact1 / fact2
  • 步骤 11: 打印 per
  • 步骤 12:结束

Java 程序

输出

Enter the Value of n and r?
5
2
nPr = 20

C程序

输出

Enter the Value of n and r?
5
2
nPr = 20

C# 程序

输出

Enter the Value of n and r?
5
2
nPr = 20

Python 程序

输出

Enter the value of n:
5
Enter the value of r:
2
nPr = 20
下一主题#