Guest User

Untitled

a guest
Jun 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import java.util.*;
  4.  
  5. public class Viimeinen {
  6.  
  7. public static ArrayList<Integer> asetaNumerot(int luvut) {
  8. ArrayList<Integer> taulu = new ArrayList<Integer>();
  9. for (int i = 1; i <= luvut; i++) {
  10. taulu.add(i);
  11. }
  12. return taulu;
  13. }
  14.  
  15. public static int poista(ArrayList<Integer> taulu) {
  16. int k = 0;
  17. boolean poistuuko = false;
  18.  
  19. while (taulu.size() != 1) {
  20. k = 0;
  21. while (k < taulu.size()) {
  22. if (poistuuko) {
  23. taulu.remove(k);
  24. poistuuko = false;
  25. } else {
  26. poistuuko = true;
  27. k++;
  28. }
  29. }
  30. }
  31. return taulu.get(0);
  32. }
  33.  
  34. public static int viimeinen(int montako) {
  35. return poista(asetaNumerot(montako));
  36. }
  37.  
  38. public static void main(String[] args) {
  39. Scanner sc = new Scanner(System.in);
  40. System.out.print("Montako? ");
  41. int luku = sc.nextInt();
  42. int voittaja = viimeinen(luku);
  43. System.out.println("Voittaja: " + voittaja);
  44. }
  45. }
Add Comment
Please, Sign In to add comment