题解 | #附加题#
附加题
https://www.nowcoder.com/practice/58b04ed2865f4ff4921290f1bd4ee486
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throw IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while (line != null) {
int n = Integer.parseInt(line);
line = br.readLine();
int[] p = new int[n];
int i = 0;
for (String s : line.split(" ")) {
p[i++] = Integer.parseInt(s);
}
int[] dp = new int[n];
int res = 0;
i = 0;
while (i < n) {
dp[i]++;
if (dp[i] != 0 && dp[i] % 2 == 0) {
i++;
res++;
continue;
}
i = p[i];
}
System.out.println(res);
line = br.readLine();
}
}
}
#美团暑期实习#
import java.io.*;
public class Main {
public static void main(String[] args) throw IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while (line != null) {
int n = Integer.parseInt(line);
line = br.readLine();
int[] p = new int[n];
int i = 0;
for (String s : line.split(" ")) {
p[i++] = Integer.parseInt(s);
}
int[] dp = new int[n];
int res = 0;
i = 0;
while (i < n) {
dp[i]++;
if (dp[i] != 0 && dp[i] % 2 == 0) {
i++;
res++;
continue;
}
i = p[i];
}
System.out.println(res);
line = br.readLine();
}
}
}
#美团暑期实习#