/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package amusement; import java.util.Scanner; /** * * @author hlavaty7 */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int size; int[] pole; while (sc.hasNext()) { size = sc.nextInt(); pole = new int[size]; for (int i = 0; i < pole.length; i++) { pole[i] = sc.nextInt(); } int vykonaj = vykonaj(pole); System.out.println(vykonaj); } } private static int vykonaj(int[] pole) { int c = 0; int i = 0; while (i < pole.length) { if (pole[i] != i + 1) { int tmp = pole[i]; int index = pole[i]; pole[i] = pole[index - 1]; pole[index - 1] = tmp; c++; } if(pole[i] == i+1){ i++; } } return c; } }