找出數組中每個元素相對其他元素的大小


題目可能表述不是十分清楚,舉個例子。

假設一個數組,元素分別是3 9 2 1 8 3 2,需要輸出3 5 2 1 4 3 2,輸出中的3表示元素3在數組所有的元素中是排在第三位的,比1 2 大,5表示9在數組所有的元素中是排在第五位的,也就是最大的。

思路:首先想到的是可不可以通過各種各樣的排序方法解決這個問題,我們知道在排序的時候,元素的位置信息是不被保留的,但是這里的輸出要求按元素在數組中原始的排列順序輸出。我們可以在排序算法的基礎上稍作修改就okay了。下面的實現借鑒了冒泡排序的思想。

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;

/*repeat exception class*/
class RepeatException extends Exception{    

    private static final long serialVersionUID = 1L;
    
    static String str = "elements can not repeat!!!";
    
    /*constructor*/
    RepeatException()
    {
        super.printStackTrace();
        System.out.println(str);
    }

}

public class WhatIsMyNumber {
    /**
     * 
     * @param al -- ArrayList store elements in original double array
     * @param positionInfo -- ArrayList store position information, for example, al = {1,2,1}
     * then, positionInfo = {0,1,0}, the second 0 means the relative order of this elements is
     * store in the first position of result (return by computeNumber)
     * @return
     */
    public static ArrayList<Double> deleteRepeat(ArrayList<Double> al, ArrayList<Integer> positionInfo){
        
        ArrayList<Double> temp = new ArrayList<Double>();
        //flag record position information
        int flag = 0;
        
        for(int i = 0; i < al.size(); i++)
        {
            if(temp.contains(al.get(i)))
            {
                positionInfo.add(temp.indexOf(al.get(i)));
            }
            else
            {
                positionInfo.add(flag);
                flag++;
                temp.add(al.get(i));
            }
        }
        return temp;
    }
    
    /**
     * 
     * @param noRepeatArray ArrayList after delete repeat elements
     * @return return the relative order of every element in noRepeatArray
     * @throws Exception throw exception if repeat elements are found in noRepeatArray
     */
    public static int[] computeNumber(ArrayList<Double> noRepeatArray) throws Exception{
        
        int[] result = new int[noRepeatArray.size()];
        for(int i = 0; i < result.length; i++)
        {
            result[i] = 1;
        }
        
        for(int i = 0; i < noRepeatArray.size(); i++)
        {
            for(int j = i+1; j < noRepeatArray.size(); j++)
            {
                if(noRepeatArray.get(i) < noRepeatArray.get(j))
                    result[j]++;
                if(noRepeatArray.get(i) > noRepeatArray.get(j))
                    result[i]++;
                if(noRepeatArray.get(i) == noRepeatArray.get(j))
                    throw new RepeatException();
            }
        }
        
        return result;
    }
    
    public static void outputResult(double[] test){
        
        double[] output = new double[test.length];
        ArrayList<Double> al = new ArrayList<Double>();
        ArrayList<Integer> positionInfo = new ArrayList<Integer>();
        for(int i = 0; i < test.length; i++)
        {
            al.add(test[i]);
        }
          
        al = deleteRepeat(al,positionInfo);
        try
        {
            int[] result = computeNumber(al);
            for(int i = 0; i < output.length; i++)
            {
                output[i] = result[positionInfo.get(i)];
                System.out.print(output[i] + " ");
            }
            
            System.out.println();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args){
        
        double[] test = new double[]{8,8,8,2.5,8,8,2.5,8,13,15,12,2.5,15,2.5,8,15};
        outputResult(test);
    }
下面來說說程序的主要思想。
給定一個數組,假設是{5,2,4,1},輸出數組初始化為{1,1,1,1},然后5和2比較,5>2,輸出數組變為{2,1,1,1},5和4比較,5>4,輸出數組變為{3,1,1,1},5和1比較,5>1,輸出數組變為{4,1,1,1},這是一輪,下一輪,2分別與4和1比較,最后是4和1比較。最后輸出數組是{4,2,3,1}。

如果數組中存在重復的元素,兩個一樣大小的元素得到的relative order可能是不一致的,所以在實現的時候書寫了
deleteRepeat這樣一個函數,並且用positionInfo來保存位置信息。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM