日期:2014-05-20 浏览次数:20695 次
public static void main(String[] args) throws Exception {
int[] arr = new int[]{0,1,2,4,5,6,9,10};
String s = "" + arr[0];
int last = arr[0];
for(int i = 1; i < arr.length;i++){
if(arr[i] - last == 1){
;
}else{
System.out.println(s + "-" + last);
s = "" + arr[i];
}
last = arr[i];
}
if(!s.equals("" + last)){
System.out.println(s + "-" + last);
}
}
public class Test03 {
static final int[] arr = new int[] { 0, 1, 2, 4, 5, 6, 9, 10 };
public static void main(String[] args) {
takeOut(arr);
}
static void takeOut(int[] arr) {
//假设数组是连续的
int start = 0, end = start;
for (int i = 1; i < arr.length; ++i) {
if (arr[i] - arr[end] == 1) {
end = i;
} else {
System.out.printf("%d-%d\n", arr[start], arr[end]);
start = i;
end = start;
}
}
//打印最后一组数据
System.out.printf("%d-%d\n", arr[start], arr[end]);
}
}
public class Test {
public static void main(String[] args)
{
int[] i = {0, 1, 2, 4, 5, 6, 9, 10};
int tmp = -1;
for (int j = 0; j < i.length; j++) {