java题目好难啊?求大侠解答,谢谢、、 设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java题目好难啊?求大侠解答,谢谢、、 设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合。相关的知识,希望对你有一定的参考价值。

例如从1..4中选取2个元素的所有组合的输出结果为:1,2 1,3 1,4 2,2 2,3 2,4 3,4.
谢谢大家 不过要回答的问题是:设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合。

这是我以前写的 改的
main中注释部分是原来的,下面两句是按你的需求改的
目前是取3个元素

//列出集合的所有组合1,2 121,2 这里使用数组方便取值
public class ListAll

public static void main(String[] args)

int[] arr = new int[5];
//为数组赋初值
for (int i = 0; i < arr.length; i++)
arr[i] = i + 1;

//开始递归 从下表0开始 依次取0,1,2...个元素 存到list中
// for (int i = 0; i <= arr.length; i++)
//
// List<Integer> l = new ArrayList<Integer>();
// start(arr, 0, i, l);
//
List<Integer> l = new ArrayList<Integer>();//
start(arr, 0, 3, l);//只去三个 就把上面的i改成3


private static void start(int[] arr, int index, int num, List<Integer> l)

if (num == 0)//如果需要取的个数为0 说明取值完毕 输出 退出此次取值

System.out.println(l);
return;


for (int i = index; i < arr.length - num + 1; i++)//关键。。

l.add(arr[i]);
start(arr, i + 1, num - 1, l);
l.remove(l.size() - 1);






[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]追问

你写的最好了 不过问题是:设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合?要再次麻烦你了 ,我只有15分 全给你。

追答

你太明白你的意思。。。
是要k取所有值的所有组合吗
那就是程序的原意了
//开始递归 从下表0开始 依次取0,1,2...个元素 存到list中
// for (int i = 0; i l = new ArrayList();
// start(arr, 0, i, l);
//
List l = new ArrayList();//
start(arr, 0, 3, l);//只去三个 就把上面的i改成3

去掉上面注释和下面两行 就是源程序

追问

运行不了 你把原程序发到479846446@qq.com xiexie

追答

怎么会呢 已发 试试

参考技术A 看了你的题后就自己写了个测了一下、符合你的问题:
public class Test
public static void main(String[] args) throws Exception
//这里是随便来个数组测试
int[] arr = new int[] 5, 6, 7, 8, 9 ;
for (int i = 0; i < arr.length; i++) //两层for循环就能解决,
for (int j = i + 1; j < arr.length; j++) //重点是j=i+1,避免相同数重复
System.out.print(arr[i] + ", " + arr[j] + " ");

System.out.println();




//既然我回答了,那么我多问一句,你Java学了多久了?这题说真的……
你看我的代码也就那么几句吧!
我也注释了,没问题吧。
帮你搞定我也要睡觉了,明天要上班喽……追问

问题是:设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合?你很厉害啊,帮帮我

追答

看了LZ追问和别人的回答,知道俺粗心了,抱歉了...........
花了些时间自己现写了个程序,这次应该满足LZ的要求了吧!.............

public class Test

// 集合1...n元素,我选了6个
static int[] arr = new int[] 1, 6, 7, 8 ,10,17;
// 选取K个,就选3个吧
static int k =3 ;
public static void main(String[] args)
if(k == 0)
System.out.println("你有没有搞错!");
return;

int[] out = new int[k];
int flag = 0;
count(arr.length,flag, out);


static void count(int len, int flag, int[] out)
int j;
for ( j = flag; j 0)
if(out[flag] <= out[flag-1])
continue;


if(flag == k-1 )
for(int i=0; i<out.length; i++)
System.out.print(out[i] + ", ");

System.out.print(" ");

else
int newflag = flag+1;
count(len, newflag, out);
System.out.println();






//我还要多说两句:认真看了之后,难度上升了不少,总体解决方法就递归法,
//我觉得最大的难度时细节的处理...粗心就花了不少时间, 由于时间有限,没多注释,如果LZ想要注释的话说一声,我回去后再加上去^ ^......快下班了!(总之代码运行正常!由于时间仓促,说不定有的地方没注意到.).........又看了下,还可以优化一些地方,

本回答被提问者采纳
参考技术B 楼上搞笑了,LZ那个只是个例子,你反而将个特例写了出来,连题目都没搞清楚。这题的意思是随便弄个数组,里面有N个数,然后要你定义个方法,方法的参数是K(这个也是自定义),然后方法根据你实际输入的参数将所有的组合列出来,返回个值给你。重点就是要写出个具有普遍性的方法。 参考技术C class Test
public static void main(String[] args)
int[] a=1,2,3,4;
for(int i=0,i<a[i].length;i++)
for(int j=i+1;j<i+4,j<a[i].length;j++)
if(a[i]!=a[j])
syso(a[i],a[j]);






//楼主是这个意思吗追问

在看看问题写好一点吗 很显然你会啊

急求翻译 小女子在这里先谢谢哪位大侠了

Energetic, environmental and economic balances: Spice up your ionic liquid research efficiency

Dana Kralisch, Annegret Stark, Swen Körsten, Günter Kreisel and Bernd Ondruschka

keywords:
The energy requirement, environmental impact and material costs of the synthesis of ionic liquids, and of their subsequent use as reaction media in the metathesis of 1-octene, are compared to conventional solvents. This preliminary study lays the foundation for an ecological and strategic experimental design. Energetic, environmental and economic assessments over all life-cycle stages allow for the identification of both, disadvantages and opportunities of individual process steps, at an early R&D level. Thus, this approach helps to find new and improved solutions, which comply with the concepts of Green Chemistry, that cannot be determined by experimental work alone. The potential of innovative methods can be quantitatively compared to current technologies by means of the energy efficiency factor EEF. Interestingly, this study demonstrates that under certain circumstances, a solvent-free reaction mode may not necessarily be ecologically advantageous. Also, the presumption that, due to facile recycling, a bi-phasic reaction mode is always superior to a homogeneous one is questioned: compared to the energy required for the manufacture of a solvent which results in a biphasic reaction mode (e.g. an ionic liquid), the energy needed for the separation of a homogeneous reaction mixture by distillation is comparatively small. Thus, efficient recycling of such a solvent must be guaranteed.

能源,环境和经济的平衡:加速您有关离子液体的研究效率

德纳kralisch , annegret斯塔克, swen körsten , günter kreisel和贝恩德ondruschka

关键词:
能源的要求,环境的影响以及材料成本考虑,合成离子液体,及其利用作为反应介质,在分解1 -辛烯,是比较传统的溶剂。这一初步研究奠定了基础生态和战略的实验设计。充满活力,环境和经济评估,所有的生命周期阶段,以便确定双方的,缺点和机会,个别的过程中的步骤,在早期的研发水平。因此,这种方法有助于找到新的和改进的解决方案,遵守的概念,绿色化学,这是不能确定的试点工作。的潜力,创新的方法,可以定量比较,目前的技术手段,能源效率的因素eef 。有趣的是,这项研究表明,在某些情况下,一无溶剂的反应模式未必是生态优势。此外,推定,由于容易回收,双向阶段性的反应模式是始终优于均匀,一个是质疑:相比,所需的能量用于制造溶剂,结果在一个双相反应模式(如1离子液体) ,能源需要分离的一均相反应混合物的蒸馏是比较少。因此,有效的回收利用等溶剂,必须得到保证。
参考技术A 有点难 是不是写论文啊?我刚交过论文了 现在这没时间帮你哈 在线翻译以后再根据理解加工吧

以上是关于java题目好难啊?求大侠解答,谢谢、、 设计算法求解从集合1...n中选取k(k<=n)个元素的所有组合。的主要内容,如果未能解决你的问题,请参考以下文章

Android launcher3中系统应用程序的图标在哪里,怎么改成自己的图标?求大侠解答 谢谢

JAVA语言程序设计题目,求高人解答

java编程语言谁会,下面题目帮解答一下,要详细步骤啊…………谢谢 急急急求……

java中toString类题目求解答

递归的都好难啊!QAQ

我真的好难啊!越来越难了