C(n,k)在n个不重复数中获得k个数
//比如在数组a[]={1,7,89,87} 中k=2的时候 组合为 C(4,2)=6 package 再次开始; import java.util.ArrayList; //本次实现的是在n个不重复树中提取k个;输出各种组合
//在 1 2 3 7 8 public class 组合问题 {
public static void main(String args[])
{ int arr[]=new int[]{1,3,4,6,9};//
ArrayList<Integer> arry=new ArrayList<Integer>();//用于保存当前的求得的序列 dfs(arr,arry, 3, 0); }
// arry 原始排序数组, k表示取多少个,start表示当前可以选取的下标
private static void dfs(int[] arr,ArrayList<Integer> arry, int k, int start) {
// TODO Auto-generated method stub if(arry.size()==k)
{
System.out.println(arry);
return; }
else
{
for(int i=start;i<arr.length;i++)
{
arry.add(arr[i]);
dfs(arr,arry,k,i+1);
arry.remove(arry.size()-1); } } } }
ref:http://blog.csdn.net/u010500263/article/details/18435495
int arr[]=new int[]{1,3,4,6,9};取得3个 [1, 3, 4]
[1, 3, 6]
[1, 3, 9]
[1, 4, 6]
[1, 4, 9]
[1, 6, 9]
[3, 4, 6]
[3, 4, 9]
[3, 6, 9]
[4, 6, 9]
C(n,k)在n个不重复数中获得k个数的更多相关文章
- [Swift]LeetCode668. 乘法表中第k小的数 | Kth Smallest Number in Multiplication Table
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number ...
- Leetcode 668.乘法表中第k小的数
乘法表中第k小的数 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要返回表中第k 小的数字. 例 1: 输入 ...
- LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13
378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...
- Java实现 LeetCode 668 乘法表中第k小的数(二分)
668. 乘法表中第k小的数 几乎每一个人都用 乘法表.但是你能在乘法表中快速找到第k小的数字吗? 给定高度m .宽度n 的一张 m * n的乘法表,以及正整数k,你需要返回表中第k 小的数字. 例 ...
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- 线性时间O(n)内求数组中第k大小的数
--本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...
- 数组中第K小的数字(Google面试题)
http://ac.jobdu.com/problem.php?pid=1534 题目1534:数组中第K小的数字 时间限制:2 秒 内存限制:128 兆 特殊判题:否 提交:1120 解决:208 ...
随机推荐
- lispbox 安装运行.sh的时候出现 lispbox.sh: 2: lispbox.sh: Bad substitution
安装lispbox时使用tar命令将压缩文件解压之后cd进入之后在运行.sh文件时出现了如下情况. $ sh lispbox.sh lispbox.: lispbox.sh: Bad substitu ...
- Reactor模式(反应器模式)
Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...
- 树莓派2 安装mono3.0运行mvc4
sudo apt-get updatesudo apt-get upgradesudo apt-get mono-completewget -c http://www.linuxdot.net/dow ...
- 网络请求 post 的接受请求头的代理方法
接收响应头 content-Length content-Type - (void)connection:(NSURLConnection *)connection didReceiveRespons ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- java单例模式使用及注意事项
1. 说明 1)单例模式:确保一个类只有一个实例,自行实例化并向系统提供这个实例 2)单例模式分类:饿单例模式(类加载时实例化一个对象给自己的引用),懒单例模式(调用取得实例的方法如getInstan ...
- 按行N等分某个文件
# --*-- coding:utf-8 --*--import randomimport math def fanhui(): into = random.randint(1, 10) ...
- centos SSH配置详解
基本概念 linux自带的ssh为OpenSSH ssh协议提供两种用户认证方式 1. 基于口令的安全认证:使用用户名.密码的方式对登录用户进行认证 2. 基于密钥的安全认证:使用公钥和私钥对的方 ...
- RandomAccessFile类的使用(随机读取java中的文件)
package coreJava; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; ...
- jquery and event
jquery阻止事件冒泡 event.stopPropagation(); event.cancelBubble = true; jquery阻止默认操作 event.preventDefault() ...