Java implements 25 horses in the algorithm, find the fastest 3, but only 5 tracks, each game can only be sorted by 5 horses, then how many times do you need to match?

tags: algorithm  java  Sorting Algorithm

Java implements 25 horses in the algorithm, find the fastest 3, but only 5 tracks, each game can only be sorted by 5 horses, then how many times do you need to match?

  • Random number simulation 25 horses
import java.lang.reflect.Array;
import java.util.Arrays;

public class Ma {
    public static void main(String[] args) {
        double[] a1[] = new double[5][5];
        double[] a2[] = new double[5][3];
        double[] a3[] = new double[5][3];
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
        System.out.println ("25th Ratio");
        for (int i = 0; i < a1.length; i++) {
            for (int i1 = 0; i1 < a1[i].length; i1++) {
                a1[i][i1] = Math.random() * 100;
                System.out.print(a1[i][i1] + "\t\t");
            }
            System.out.println();
        }
        for (int i = 0; i < a1.length; i++) {

            Arrays.sort(a1[i]);

        }
        double m;
        for (int i = 0; i < a2.length; i++) {
            for (int i1 = 0; i1 < a2[i].length; i1++) {
                m = a1[i][a1[i].length - 1 - i1];
                a2[i][i1] = m;

            }

        }
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
                 System.out.println ("Range of 25 Runa 5 after the game");
        for (int i = 0; i < a1.length; i++) {
            for (int i1 = 0; i1 < a1[i].length; i1++) {

                System.out.print(a1[i][i1] + "\t\t");
            }
            System.out.println();
        }
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
                 System.out.println ("Take the top three of the speed of 25 raceful horse after five games");
        for (int i = 0; i < a2.length; i++) {
            for (int i1 = 0; i1 < a2[i].length; i1++) {

                System.out.print(a2[i][i1] + "\t\t");
            }
            System.out.println();
        }

        int[] z = new int[5];
                 ; // Store the subscripted array element originally subscript
                 Double [] b = new double [5]; // Store arrays that require rank sequences
                 Double [] d = new double [5]; // store B [] array after ascending
        for (int i = 0; i < 5; i++) {
            b[i] = a2[i][0];

        }
        for (int i = 0; i < b.length; i++) {
            d[i] = b[i];
        }
                 Arrays.sort (d); // Assembly arrangement
        for (int i = 0; i < d.length; i++) {
            for (int j = 0; j < d.length; j++) {
                if (d[i] == b[j]) {
                    z[i] = j;
                    b[j] = -999999;
                    break;
                } else {
                    continue;
                }
            }
        }
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
                 System.out.println ("Sixth Competition: 25 The first game of the first game of the speed of the rapids) (the member is not changed)");
        for (int i = 0; i < a3.length; i++) {
            for (int j = 0; j < a3[i].length; j++) {
                a3[i][j] = a2[z[i]][j];
                System.out.print(a3[i][j] + "\t\t");
            }
            System.out.println();
        }
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
                 System.out.Println ("Remove the first other other controversial game results");
        double[] mm = {a3[4][1], a3[4][2], a3[3][0], a3[3][1], a3[2][0]};
        Arrays.sort(mm);
        for (int i = 0; i < mm.length; i++) {
            System.out.print(mm[i] + "\t\t");
        }
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("-----------------------------------------------------------------------");
                 System.out.println ("The fastest three horses" + A3 [4] [0] + "\ t \ t" + mm [4] + "\ t" + mm [3]);


    }
}

operation result: 
-----------------------------------------------------------------------
-----------------------------------------------------------------------
 25 speed of rapping horse
41.16856306827879		40.58252617025392		67.05712863342502		67.8539567988117		70.7560619248799		
53.12588289824111		63.123216402371085		72.26841725311502		37.32135739637138		72.1084431363291		
57.54303277063529		16.54300171646218		56.4301716349477		63.46113627279707		71.48397944175716		
60.212504664887504		92.90588298272931		62.203994871534675		81.99279982567069		88.09580968326219		
11.464967736874554		84.69711834411846		89.52981943859018		57.78249880211842		47.24087381972949		
-----------------------------------------------------------------------
-----------------------------------------------------------------------
 25 raceful races of race horse after five games
40.58252617025392		41.16856306827879		67.05712863342502		67.8539567988117		70.7560619248799		
37.32135739637138		53.12588289824111		63.123216402371085		72.1084431363291		72.26841725311502		
16.54300171646218		56.4301716349477		57.54303277063529		63.46113627279707		71.48397944175716		
60.212504664887504		62.203994871534675		81.99279982567069		88.09580968326219		92.90588298272931		
11.464967736874554		47.24087381972949		57.78249880211842		84.69711834411846		89.52981943859018		
-----------------------------------------------------------------------
-----------------------------------------------------------------------
 Take the top three of the 25th rapids after five games
70.7560619248799		67.8539567988117		67.05712863342502		
72.26841725311502		72.1084431363291		63.123216402371085		
71.48397944175716		63.46113627279707		57.54303277063529		
92.90588298272931		88.09580968326219		81.99279982567069		
89.52981943859018		84.69711834411846		57.78249880211842		
-----------------------------------------------------------------------
-----------------------------------------------------------------------
 The sixth competition: 25 raceful results in the top three of the race ranking ranks (the members within the group constant)
70.7560619248799		67.8539567988117		67.05712863342502		
71.48397944175716		63.46113627279707		57.54303277063529		
72.26841725311502		72.1084431363291		63.123216402371085		
89.52981943859018		84.69711834411846		57.78249880211842		
92.90588298272931		88.09580968326219		81.99279982567069		
-----------------------------------------------------------------------
-----------------------------------------------------------------------
 Remove the first other controversial gart
72.26841725311502		81.99279982567069		84.69711834411846		88.09580968326219		89.52981943859018		-----------------------------------------------------------------------
-----------------------------------------------------------------------
 The fastest three horses are 92.90588298272931 89.52981943859018 88.09580968326219

 The process is over, exit the code 0

  • 1-5 games:
  • The 25 horses are divided into 5 groups, each group 5, get the following sort, the fastest horse in each set, ie X1, X6, X11, X16, X21 is the fastest in each group.
Group 1: x1 x2 x3 x4 x5 
 Group 2: x6 x7 x6 x6 x7 x8 x10 
 Group 3: X11 x12 x13 x14 x15 
 Group 4: x16 x17 x18 x16 x17 x18 x16 x17 x18 x16 x20 
 Group 5: X21 x22 x23 x21 x22 x23 x24 x22 x23 x24 x22 x23 x24 x25 x23 x21 x22 x23 x24 x25 
  • However, it is still not possible to say the fastest 3 horses in X1, X6, X11, X16, X21, because it is possible that the fastest 3 horses are in the first group, that is, the X2 ratio of X6 is fast

  • But we must know that the last 2 of each group will definitely not be the fastest 3 horses, then exclude X4, X5; X9, X10; X14, X15; X19, X20; X24, X25;

  • The sixth game:

    X1  X2  X3  
    X6  X7  X8 
    X11 X12 X13 
    X16 X17 X18 
    X21 X22 X23 
    
  • The first place in each group is 1, X6, X11, X16, X21, assuming speed sorting is X1, X6, X11, X16, X21.

  • Then we can know that X16, X21 and its subsequent X17, X18; X22, X23 cannot be the fastest 3 horses.

  • Section 7:

X1  X2  X3  
X6  X7  X8 
X11 X12 X13 
  • At present, we can know that X1 is the fastest in 25 horses, but the speed between X2, X6, X3, X7, X11 is not sure, need to play again, and X8, x12, x13 is the fastest The top three.
  • The competition is: X2, X6, X3, X7, X11, the fastest speed 2 plus X1 makes the fastest 3 horses.

Intelligent Recommendation

64 horses, 8 tracks, the least number of races to find the fastest 4 horses, and sort all the horses

Question: 64 horses, 8 tracks, at least a few races to find the fastest 4 horses, at least a few races to sort all horses Question 1: 64 horses, 8 tracks, at least a few races to find the fastest 4 ho...

25 racehorses, no stopwatch, five tracks. Use the least number of races to find the three fastest horses. Process analysis

The above are my personal thoughts, there is a better way to leave a message, thank you....

Tencent Interview: 64 horses, 8 tracks, find the fastest 4 minimum?

Click on the blue word to pay attention to me, rise in knowledge 01 Story origin There are 64 horses, 8 tracks, to find the fastest 4 horses, at least a few times? 02 Initial thinking Many students ma...

Drawing analysis: 25 horses, divided into 5 groups each time, the top three can be selected in a few games

Article directory start Pick out the first place: Pick out the second place: pick third place start First we have 25 horses, marked as A1 ~ A5, B1 ~ B5, C1 ~ C5, D1 ~ D5, E1 ~ E5 We compare vertically...

More Recommendation

64 horses, 8 tracks, find the 4 fastest horses (another idea) (10 steps or 11 steps)

Steps 1-8: Divide 64 horses into 8 groups and race 8 times to find the fastest 8 horses in each group. 9 steps: Race with the fastest horses in each group and find the 4 fastest horses from groups A, ...

Tencent interview questions: 64 horses, 8 tracks, find the four fastest running horses.

64 horses, 8 tracks, find the four fastest running horses, at least how many races are needed? first step: The second step: third step: the fourth step: supplement: first step: Divide 64 horses into e...

64 horses, 8 tracks, to find the fastest 4 horses, at least 9 games

In the case of this problem, don't try to think about the specific way.First use algorithms to find the lower limitNext, the next decrease is shortened by verification and analysis, because your upper...

[64] Detailed Tencent face questions horses need at least eight track how many rounds to elect the fastest 4 horses?

topic:64 horses requires a minimum of eight track how many rounds to elect the fastest 4 horses? Topic analysis:The meaning of the title itself is unclear, but since it is the programmer interview que...

64 horses, 8 tracks, find out how many games in the top 4 at least?

in conclusion: Minimum 10 times, maximum 11 times first step All horses are divided into 8 groups, each running once, and then the last four of each group are eliminated. Need here8 times。 Second step...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top