9
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class 连号区间数 { public static List<Integer> list = new ArrayList<Integer>(); public static List<Integer> list2 = new ArrayList<Integer>(); public static int m = 0; public static StringBuffer sb = new StringBuffer(); public static int count = 0; public static void main(String[] args) { Scanner scan = new Scanner(System.in); m = Integer.parseInt(scan.nextLine()); Scanner s = new Scanner(scan.nextLine()); while (s.hasNextInt()) { list.add(s.nextInt()); } list2 = new ArrayList<Integer>(list); go(0); System.out.println(count+m); } public static void go(int index) { if (index == 2) { if (sb.charAt(0) < sb.charAt(1)) { judge(sb); } } else { for (int i = 0; i < list.size(); i++) { int temp = list.get(i); sb.append(temp); list.remove(i); go(index + 1); list.add(i, temp); sb.deleteCharAt(sb.length() - 1); } } } private static void judge(StringBuffer sb) { int L = sb.charAt(0)-48; int R = sb.charAt(1)-48; getMinAndMax(L, R); } private static void getMinAndMax(int L, int R) { int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE; for(int i=L-1; i<=R-1; i++) { if(list2.get(i)<min) { min = list2.get(i); } if(list2.get(i)>max) { max = list2.get(i); } } if((max-min) == (R-L)) { count ++; } } }
原文地址:http://blog.csdn.net/tracysilocean/article/details/26280015