求数组的top n的下标,有缺点

/**
  * an fault : topN(int[],int) method will
  * change the value of the argument int[]
  *
  */
package com.yatium;
import java.util.*;

public class TestArray{
    /* exchange two element with given indexes of an array */
    public static void exchEleArr(int[] arr,int index1,int index2){
        int temp=arr[index1];
        arr[index1]=arr[index2];
        arr[index2]=temp;
    }
    /* sort by descend of an array */
    public static void desSortArr(int[] arr){
        Arrays.sort(arr);
        int i=0;
        int j=arr.length-1;
        while(i<j){
            exchEleArr(arr,i,j);
            i++;
            j--;
        }
    }
    /* print an arry */
    public static void printArr(int[] arr){
        for(int i=0;i<arr.length;i++)
            System.out.print(arr[i]+"   ");
        System.out.println();
    }
    /*clone an array */
    public static int[] copyArr(int[] arr){
        int[] tempArr=new int[arr.length];
        for(int i=0;i<arr.length;i++) tempArr[i]=arr[i];
        return tempArr;
    }
    /* return the index of the maximum value of an array */
    public static int maxArr(int[] arr){
        int i=0,j=1;
        for(;j<arr.length;j++) if(arr[j]>arr[i]) i=j;
        return i;
    }
     /* return the index of the minimum value of an array */
    public static int minArr(int[] arr){
        int i=0,j=1;
        for(;j<arr.length;j++) if(arr[j]<arr[i]) i=j;
        return i;
    }
    /* set the value with the given index of an array */
    public static void setArr(int[] arr,int index,int value){
        if(index>=arr.length) throw new ArrayIndexOutOfBoundsException();
        arr[index]=value;
    }
    /* return an array which contains indexes of top n sort by value in an array */
    public static int[] topN(int[] arr,int n){
        int[] indexArr=new int[n];
        for(int i=0;i<n;i++){
            indexArr[i]=maxArr(arr);
            setArr(arr,maxArr(arr),arr[minArr(arr)]);
        }
        return indexArr;
    }
    /* the test */
    public static void main(String[] args){
        int[] a={6,1,6,2,6,3,7,4,8,9};
        printArr(a);
        System.out.println(maxArr(a)+"  "+minArr(a));
        int[] b=topN(a,5);
        printArr(b);
        printArr(a);
    }
}

package com.yatium;
import java.util.*;
import com.yatium.TestArray;

public class Test74{
    public static int arrToNum(int[] a,int rad){
        int num=0;
        for(int i=a.length-1;i>=0;i--)
            num+=Math.pow(rad,i)*a[i];
        return num;
    }
    public static void main(String[] args){
        int[] first=new int[4];
        int[] second=new int[4];
        int[] third=new int[4];
        for(int i=1;i<10;i++)
            for(int j=1;j<10;j++)
                for(int k=1;k<10;k++)
                    for(int l=1;l<10;l++)
                    {
                        first[0]=i;
                        first[1]=j;
                        first[2]=k;
                        first[3]=l;
                        second=TestArray.copyArr(first);
                        third=TestArray.copyArr(first);
                        TestArray.desSortArr(third);
                        Arrays.sort(second);
                        int a=arrToNum(second,10);
                        int b=arrToNum(third,10);
                        int c=arrToNum(first,10);
                        if((a-b)==c) System.out.println(c);
                    }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值