Initially, the order of cards in the deck (if you list cards from top to bottom) is {100, 50, 60, 30}. Pupkin will perform the following actions:

  1. Get a card with the number 100 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {50, 60, 30, 100}.
  2. Get a card with the number 50 and put it down the deck, since the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {60, 30, 100, 50}.
  3. Get a card with the number 60 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {30, 100, 50, 60}.
  4. Get a card with the number 30 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that will be {100, 50, 60}.
  5. Get a card with the number 100 and put it down the deck, as the number written on it is not the minimum of the remaining ones. The order of cards in the deck after that will be {50, 60, 100}.
  6. Pull out a card with the number 50 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that will be {60, 100}.
  7. Get a card with the number 60 and remove it, since the number written on it is the minimum of the remaining ones. The order of cards in the deck after that becomes {100}.
  8. Get a card with the number 100 and remove it, since the number written on it is the minimum of the remaining ones. After that, there will be no cards left in the deck.

Thus, Pupkin will look at the top card of the deck 8 times to sort the cards in it. Write code that implements all of this in Java SE. Use only one-dimensional array, for example: int array [] = {100, 50, 60, 30}; here is my implementation, but not true:

int array[] = new int[5]; array[0] = 100; array[1] = 50; array[2] = 60; array[3] = 30; array[4] = 101; for(int i = 0; i < array.length; i++){ int min = array[i]; int imin = i; for (int j = i+1; j < array.length; j++) { if(min < array[j]){ min = array[j]; imin = j; } } if (i != imin) { int temp = array[i]; array[i] = array[imin]; array[imin] = temp; } System.out.print(" "+array[i]); } 
  • four
    What attempts have been made to solve this homework? - DimXenon
  • one
    tried to do..bubble and sorting array .. did not work. I ask for help from people who can master this task. Personally, I did not. I am a novice programmer, and therefore turned here. Thank you for understanding. - dev3java September
  • Give me some time, I'm already writing the answer. - Vladimir Gamalyan
  • 2
    tried to do .. a bubble and sorting array .. did not work, add the code of your attempts, indicating exactly what did not work - Grundy
  • one
    @AndreyKonstantinovich, First, do not poke. Secondly, respect other members of the community, these are the people who can help you if you behave in accordance with our rules: ru.stackoverflow.com/help/stackexchange . Consider this a warning. - Nofate ♦

1 answer 1

Maybe not optimal, but the desired behavior performs.

 public class Deck { public static void main(String[] args){ int deck[] = {100,50,60,30}; // ΠœΡ‹ Π½Π΅ Π·Π½Π°Π΅ΠΌ сколько ΠΊΠ°Ρ€Ρ‚ Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅, Π½Π΅ Π·Π½Π°Π΅ΠΌ, какая ΠΊΠ°Ρ€Ρ‚Π° младшая (подлСТащая ΡƒΠ΄Π°Π»Π΅Π½ΠΈΡŽ). // Π£Π·Π½Π°Π΅ΠΌ, сколько ΠΊΠ°Ρ€Ρ‚ Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅. int length = deck.length; // Π‘Π΄Π΅Π»Π°Π΅ΠΌ ΠΏΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅, Ρ‡Ρ‚ΠΎ пСрвая ΠΊΠ°Ρ€Ρ‚Π° Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅ - младшая. int lowCard = deck[0]; // Π£Π·Π½Π°Π΅ΠΌ минимальноС число Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄ Π½Π°Ρ‡Π°Π»ΠΎΠΌ дСйствий. for(int card: deck){ lowCard = card <= lowCard ? card : lowCard; } // А Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ пристумим ΠΊ дСйствиям ΠΎΡ‚ Π»ΠΈΡ†Π° ΠŸΡƒΠΏΠΊΠΈΠ½Π°. while(deck[0] != 0){ System.out.println("Достал " + deck[0]); if(deck[0] <= lowCard){ System.out.println("Удаляю " + deck[0]); deck[0] = 0; // ΠŸΡ€Π΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ, Ρ‡Ρ‚ΠΎ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅ΠΉ младшСй ΠΊΠ°Ρ€Ρ‚ΠΎΠΉ Π±ΡƒΠ΄Π΅Ρ‚ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π°Ρ ΠΊΠ°Ρ€Ρ‚Π° Π² ΠΊΠΎΠ»ΠΎΠ΄Π΅. if(length > 1){ lowCard = deck[1] != 0 ? deck[1] : 0; } // Если ΠΊΠ°Ρ€Ρ‚Π° младшая - удаляСм Π΅Ρ‘ (замСняСм Π½Π° 0 ΠΈ ΠΏΠΎΠΌΠ΅Ρ‰Π°Π΅ΠΌ Π² ΠΊΠΎΠ½Π΅Ρ† массива Π² Π΄Π°Π½Π½ΠΎΠΌ случаС). for(int i = 0; i < length-1 ; i++){ // ΠŸΠ°Ρ€Π°Π»Π»Π΅Π»ΡŒΠ½ΠΎ ΠΈΡ‰Π΅ΠΌ Π½ΠΎΠ²ΡƒΡŽ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½ΡƒΡŽ ΠΊΠ°Ρ€Ρ‚Ρƒ. lowCard = (deck[i] != 0 && deck[i] <= lowCard) ? deck[i] : lowCard; deck[i] = deck[i+1]; } deck[length-1] = 0; if(length > 0) length--; } else{ System.out.println("Π£Π±ΠΈΡ€Π°ΡŽ Π½Π° Π΄Π½ΠΎ " + deck[0]); int rememberCard = deck[0]; for(int i = 0; i < length-1 ; i++){ deck[i] = deck[i+1]; } deck[length-1] = rememberCard; } } } } 
  • one
    Everything works as planned in the puzzle! Thank you so much for answering .. even though I already scored on it. but it will be interesting to see how it works in code. Thanks again for an adequate answer to my question - dev3java