Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.*;
- public class Viimeinen {
- public static ArrayList<Integer> asetaNumerot(int luvut) {
- ArrayList<Integer> taulu = new ArrayList<Integer>();
- for (int i = 1; i <= luvut; i++) {
- taulu.add(i);
- }
- return taulu;
- }
- public static int poista(ArrayList<Integer> taulu) {
- int k = 0;
- boolean poistuuko = false;
- while (taulu.size() != 1) {
- k = 0;
- while (k < taulu.size()) {
- if (poistuuko) {
- taulu.remove(k);
- poistuuko = false;
- } else {
- poistuuko = true;
- k++;
- }
- }
- }
- return taulu.get(0);
- }
- public static int viimeinen(int montako) {
- return poista(asetaNumerot(montako));
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Montako? ");
- int luku = sc.nextInt();
- int voittaja = viimeinen(luku);
- System.out.println("Voittaja: " + voittaja);
- }
- }
Add Comment
Please, Sign In to add comment