「java等距排列」java等值线

博主:adminadmin 2023-03-22 17:38:08 559

今天给各位分享java等距排列的知识,其中也会对java等值线进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java程序如何实现对字符串的排列组合问题?

import java.math.BigInteger;

import java.util.*;

public class PermutationGenerator {

private int[] a;

private BigInteger numLeft;

private BigInteger total;

public PermutationGenerator(int n) {

if (n 1) {

throw new IllegalArgumentException("Min 1");

}

a = new int[n];

total = getFactorial(n);

reset();

}

public void reset() {

for (int i = 0; i a.length; i++) {

a[i] = i;

}

numLeft = new BigInteger(total.toString());

}

public BigInteger getNumLeft() {

return numLeft;

}

public BigInteger getTotal() {

return total;

}

public boolean hasMore() {

return numLeft.compareTo(BigInteger.ZERO) == 1;

}

private static BigInteger getFactorial(int n) {

BigInteger fact = BigInteger.ONE;

for (int i = n; i 1; i--) {

fact = fact.multiply(new BigInteger(Integer.toString(i)));

}

return fact;

}

public int[] getNext() {

if (numLeft.equals(total)) {

numLeft = numLeft.subtract(BigInteger.ONE);

return a;

}

int temp;

// Find largest index j with a[j] a[j+1]

int j = a.length - 2;

while (a[j] a[j + 1]) {

j--;

}

// Find index k such that a[k] is smallest integer

// greater than a[j] to the right of a[j]

int k = a.length - 1;

while (a[j] a[k]) {

k--;

}

// Interchange a[j] and a[k]

temp = a[k];

a[k] = a[j];

a[j] = temp;

// Put tail end of permutation after jth position in increasing order

int r = a.length - 1;

int s = j + 1;

while (r s) {

temp = a[s];

a[s] = a[r];

a[r] = temp;

r--;

s++;

}

numLeft = numLeft.subtract(BigInteger.ONE);

return a;

}

//程序测试入口

public static void main(String[] args) {

int[] indices;

String[] elements = { "a", "b", "c"};

PermutationGenerator x = new PermutationGenerator(elements.length);

StringBuffer permutation;

while (x.hasMore())

{

permutation = new StringBuffer("%");

indices = x.getNext();

for (int i = 0; i indices.length; i++) {

permutation.append(elements[indices[i]]).append("%");

}

System.out.println(permutation.toString());

}

}

}

先给你一个看看!

java能够完成对坐标点的排序(按照坐标点到原点(0,0)的距离从大到小排列);

提供了接口,就请说明接口中每个方法要做什么

就这个接口而言,hasNext和next更像用来迭代的方法, 和排序没有关系。而sort方法则不接受任何参数,也没有任何返回值

java中,排序1,10,12,3,2,4,5。排列出来为1,10,12,2,3,4,5.代码怎么写。

整数转成字符串后排序。

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class Test {

  public static void main(String[] args) {

    ListString list = Arrays.asList("1", "10", "12", "3", "2", "4", "5");

    Collections.sort(list);

    System.out.println(Arrays.toString(list.toArray()));

  }

}

Java 关于排列 (sort)

程序整体错误较多,但你的思路很清晰。在以后要多注意数组边界的处理和排序的逻辑性思维的锻炼

出现的错误已更正,需要注意的地方代码中已经指出。希望对你有帮助

——————————————程序代码——————————————

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class population {

static Scanner console=new Scanner(System.in);

public static void main(String[]args)

{ int[] x={1,25,7,6,8}; //x.length=5;

int i,j,f;

int []v=new int[8];

for (j=0;j5;j++)/*由于x有5个元素,j=1开始循环的话那么j6,但你

后边要给数组赋值所以只有改j的初始值了,

后边就将j+1就不会出现“x0”

*/

{System.out.println("Enter the number for x"+ (j +1));

for (i=0;i8;i++)

v[i]=console.nextInt();

x[j]=(v[0]+v[1])-(v[2]+v[3])+(v[4]+v[5])+(v[6]+v[7]);

}

System.out.println("排序前:");

for (j=0;j5;j++){

System.out.println("x"+j+"=" + x[j]);

}

for (f =0; fx.length;f++)

{

for (j=0;j(x.length-1);j++)/*循环每一个元素所以要从j=0开始,

在这个数组里 第4此循环比较x[3]和 x[4], 第5次循环

只取的最后一个元素 这也是冒泡排序

要注意的地方,最后一次不要比较。*/

{

if (x[j]x[j+1])

{ //将x[j],[j+1]交换位置,

int tmp=0;

tmp=x[j+1];

x[j+1]=x[j];

x[j]=tmp;

}

}

}

System.out.println("排序后:");

for (j=0;j5;j++){

System.out.println("x"+j+"=" + x[j]);

}

}

}

________________________________程序执行结果————————————

Enter the number for x1

1 2 3 4 5 6 7 8

Enter the number for x2

2 5 8 3 6 9 7 8

Enter the number for x3

1 4 7 2 5 8 3 6

Enter the number for x4

4 5 6 7 8 92 5

4

Enter the number for x5

4 5 6 8 5 2 3 6

排序前:

x0=22

x1=26

x2=18

x3=105

x4=11

排序后:

x0=105

x1=26

x2=22

x3=18

x4=11

java实现排列组合输出

完成了一种实现,发给你参考下。

不过感觉应该还有更好的办法,有时间我会继续研究下.

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class TestQiuhe {

    //集合a{1,2,3,5,7,10},输出不多于4个元素(不重复)的加和为22的组合。

    public static void main(String[] args) {

        int[] nums={1,2,3,5,7,10};

        int l = nums.length;

        Listint[] results=new ArrayListint[]();

        for(int i1=0;i1l;i1++){

            for(int i2=0;i2l;i2++){

                if(nums[i1]==22){

                    results.add(new int[]{nums[i1]});

                }

                if(i2!=i1){

                    if(nums[i1]+nums[i2]==22){

                        results.add(new int[]{nums[i1],nums[i2]});

                    }

                    for(int i3=0;i3l;i3++){

                        if(i3!=i1  i3!=i2){

                            if(nums[i1]+nums[i2]+nums[i3]==22){

                                results.add(new int[]{nums[i1],nums[i2],nums[i3]});

                            }

                            for(int i4=0;i4l;i4++){

                                if(i4!=i1  i4!= i2  i4!=i3){

                                    if(nums[i1]+nums[i2]+nums[i3]+nums[i4]==22){

                                        results.add(new int[]{nums[i1],nums[i2],nums[i3],nums[i4]});

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

        //去重

        SetString reSet=new HashSet();

        for(int[] r:results){

            Arrays.sort(r);

            reSet.add(Arrays.toString(r));

        }

        System.out.println("一共得到结果集:"+reSet.size());

        System.out.println(reSet);

    }

}

运行结果:

一共得到结果集:2

[[5, 7, 10], [2, 3, 7, 10]]

Java开发idea如何让class的属性按长度从长到短排列整齐?

FormatFieldPyramid 这个插件可以将Java文件的全局字段排序,按代码的长度由短到长排序。

不选中内容,默认格式化整个文件,这时idea会自动根据属性的修饰符分组。选中内容,只会格式化选中内容,而且完全按长度排序,不分组,建议选中内容格式化。

java等距排列的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java等值线、java等距排列的信息别忘了在本站进行查找喔。