import java.util.*;

public class n00883428
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);
      {

         int i;
         int j;
         int numPeople;
         int numCount;
         int upTo = 0;

         long memory;
         long startTime;
         long endTime;

         System.out.print("How many people are in the circle?");
         numPeople = in.nextInt();

         System.out.print("Enter number to count out of circle:");
         numCount = in.nextInt();

         int men[] = new int[numPeople];
         System.out.print("Order of Elimination:");

            for(i = 1; i < numPeople; i++)
               men[i] = i + 1;

         startTime = System.currentTimeMillis();

         while(numPeople > 1)
         {
            upTo = (upTo + numCount) % numPeople;

            System.out.print(men[upTo] + " ");
            numPeople--;

            for(j = upTo; j < numPeople; j++)
               men[j] = men[j + 1];
         }

         System.out.println("\nNumer " +men[0] +" will be left");

       }
     }
}                     



         startTime = System.currentTimeMillis();

         while(numPeople > 1)
         {
            upTo = (upTo + numCount) % numPeople;

            System.out.print(men[upTo] + " ");
            numPeople--;

            for(j = upTo; j < numPeople; j++)
               men[j] = men[j + 1];
         }

         System.out.println("\nNumer " +men[0] +" will be left");

       }
     }
}                     

Recommended Answers

All 2 Replies

So, show what you would do. Then we can help you. The main thing is whether you want a doubly linked list (each node points to the previous and the next), or a singly linked list (each node just points to the next). It either case, the last node has to have a link to the first node.

this is what I have. My problem is, the numbers are not printing in the order that they are suppose to come out in. 
For example, if I was to type in 7 1 3. the results should be 416573 
being eliminated with 2 being the last number left.  Thanks again in advance


import java.util.*;

public class n00883428
{
   public static void main(String[] args)
   {
      int i = 0;
      int j = 0;
      int numPeople;
      int numCount;
      int max;
      int startedAt;

      int[] men = new int[25];

      Scanner in = new Scanner(System.in);

      System.out.print("How many people are in the circle?");
      numPeople = in.nextInt();

      System.out.print("Enter number to count out of circle:");
      numCount = in.nextInt();


      for(i = 0; i < numPeople - 1; i++)
         men[i] = i + 1;
         men[numPeople - 1] = 0;

         startedAt = numPeople - 1;

         max = 0;

         do
         {
            while(j != numCount)
            {
               j++;

               startedAt = max;
               max = men[startedAt];
            }

            j = 0;

            if(startedAt != max)

            System.out.println("Person " + (max + 1) +" is removed");
            men[startedAt] = men[max];
            max = men[startedAt];

         }
               while(startedAt != max);
               System.out.println("Person " + (max + 1) +" is left");
    }
 }      
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.