60. 第k个排列

给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。

按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:

"123"
"132"
"213"
"231"
"312"
"321"
给定 n 和 k,返回第 k 个排列。

说明:

给定 n 的范围是 [1, 9]。
给定 k 的范围是[1,  n!]。
示例 1:

输入: n = 3, k = 3
输出: "213"
示例 2:

输入: n = 4, k = 9
输出: "2314"


来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/permutation-sequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路见题解:https://leetcode-cn.com/problems/permutation-sequence/solution/hui-su-jian-zhi-python-dai-ma-java-dai-ma-by-liwei/

 1 public class Solution {
 2     private boolean[] flag = null;
 3     private boolean ok = false;
 4     private char[] res = null;
 5 
 6     private int jie(int n){
 7         if (n < 0) return -1;
 8         int cnt = 1;
 9         while (n > 0){
10             cnt *= n--;
11         }
12         return cnt;
13     }
14 
15     // count:未使用数字的个数
16     private boolean helper(int n, int count, int k){
17         if (count == 0) {
18             ok = true;
19             return true;
20         }
21         for (int i = 1; i <= n; i++) {
22             if (!flag[i]) {
23                 // 计算分支i的序列个数
24                 int cnt = jie(count-1);
25                 if (k > cnt) {
26                     k -= cnt;
27                     continue;
28                 }
29                 // k <= cnt 进入当前分支i
30                 flag[i] = true;
31                 res[n-count] = (char)(i+'0');
32                 if (helper(n,count-1,k))
33                     return true;
34             }
35         }
36         return false;
37     }
38 
39     public String getPermutation(int n, int k) {
40         flag = new boolean[n+1];
41         res = new char[n];
42         helper(n, n, k);
43         return new String(res);
44     }
45 
46     public static void main(String[] args) {
47         String permutation = new Solution().getPermutation(3,3);
48         System.out.println("permutation = " + permutation);
49     }
50 }

 

转载于:https://www.cnblogs.com/yfs123456/p/11614249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 题目描述: 有n个同学,每个同学有m门课程的成绩。现在要求找出总分排名第k的同学,并输出该同学的m门课程的成绩。 解题思路: 首先,我们需要计算每个同学的总分,并将其按照总分从高到低排序。然后,我们只需要输出排名第k的同学的m门课程的成绩即可。 具体实现: 1. 定义一个结构体,用来存储每个同学的成绩信息。 struct Student { int id; // 学生编号 int totalScore; // 总分 vector<int> scores; // 每门课程的成绩 }; 2. 定义一个比较函数,用来按照总分从高到低排序。 bool cmp(const Student& a, const Student& b) { return a.totalScore > b.totalScore; } 3. 计算每个同学的总分,并按照总分从高到低排序。 vector<Student> students(n); for (int i = ; i < n; i++) { students[i].id = i + 1; students[i].scores.resize(m); for (int j = ; j < m; j++) { cin >> students[i].scores[j]; students[i].totalScore += students[i].scores[j]; } } sort(students.begin(), students.end(), cmp); 4. 输出排名第k的同学的m门课程的成绩。 for (int i = ; i < m; i++) { cout << students[k - 1].scores[i] << " "; } cout << endl; 完整代码: ### 回答2: 题目要求我们找到总分排名第k的同学,那么我们首先需要计算出每个同学的总分,然后按照总分从高到低进行排序。最后找到排名为k的同学并输出其每门课程的成绩。 具体实现可以采用以下步骤: 1. 根据输入的数据,定义一个n*m的二维数组score,其中第i行表示第i个同学的m门课程成绩。 2. 计算每个同学的总分并储存在一个长度为n的一维数组total中。 3. 对total数组进行降序排序,同时将对应的score数组的行也进行相同的调整。 4. 找到排名为k的同学,即为total数组中的第k-1个元素,对应的score数组的第k-1行即为该同学的课程成绩。 5. 输出该同学的课程成绩即可。 以下是代码实现(假设n=3,m=4,k=2): ```python n, m, k = 3, 4, 2 # 输入数据 score = [[80, 90, 75, 85], [95, 88, 92, 89], [77, 83, 88, 76]] # 输入每个同学的成绩 total = [] # 存储每个同学的总分 for i in range(n): total.append(sum(score[i])) # 对total数组进行降序排序,同时调整对应的score数组的行 for i in range(n-1): for j in range(i+1, n): if total[i] < total[j]: total[i], total[j] = total[j], total[i] score[i], score[j] = score[j], score[i] # 找到排名为k的同学,输出其课程成绩 print("排名第%d名的同学的成绩为:" % k) for i in range(m): print(score[k-1][i], end=' ') ``` 输出为:`排名第2名的同学的成绩为:95 88 92 89`。 注意,以上代码只适用于保证所有同学总分互不相同的情况下,如果存在相同的总分,需要对程序进行修改。 ### 回答3: 这是一道寻找排名的问题,需要对同学的总分进行排名,并获取排名第k名同学的成绩信息。 首先需要计算每个同学的总分,将其存储在一个数组中。对这个数组进行从大到小的排序,得到排名列表。找到排名列表中第k个同学,即可输出该同学的m门课程成绩。 代码如下: ``` #include <iostream> #include <algorithm> using namespace std; const int N = 105; struct Student { int id; int score[N]; int total; }; bool cmp(Student a, Student b) { if(a.total != b.total) { return a.total > b.total; } return a.id < b.id; } int main() { int n, m, k; cin >> n >> m >> k; Student stu[N]; for(int i = 0; i < n; i++) { stu[i].id = i + 1; for(int j = 0; j < m; j++) { cin >> stu[i].score[j]; stu[i].total += stu[i].score[j]; } } sort(stu, stu + n, cmp); //按总分从大到小排序 cout << stu[k - 1].id << endl; for(int j = 0; j < m; j++) { cout << stu[k - 1].score[j] << " "; } cout << endl; return 0; } ``` 假设输入的数据为: 5 3 2 80 90 70 70 80 90 60 70 80 90 80 70 85 75 95 其输出结果为: 4 90 80 70 也就是要求排名第二的同学是编号为4的同学,其三门成绩为90、80、70。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值