只要不比前一个数大1,区间总数就加1:
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(), r = 1; int[] v = new int[n]; for(int i = 0; i < n; ++i) v[i] = sc.nextInt(); for(int i = 1; i < n; ++i){ if(v[i] != v[i - 1] + 1) r++; } System.out.println(r); } }