题目要求如下输入12345234568923456789输出
12345
23456
89
23456789
即把一个串从数字不连续的位置断开。试完成之。
package com.bird.test; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { private static List<Integer> list = new ArrayList<Integer>(); private static List<Integer> listTemp = new ArrayList<Integer>(); public static void test(String num){ for(int i = 0; i < num.length(); i++){ list.add(Integer.valueOf(num.charAt(i)-'0')); } int i = 0; listTemp.add(list.remove(0)); while(true){ if(list.size() == 0)break; if(list.get(0) - listTemp.get(i) == 1){ // System.out.println(list.get(0)+"---"+listTemp.get(i)); listTemp.add(list.remove(0)); i = i + 1; }else if(list.size() != 0){ for(int t : listTemp) System.out.print(t); System.out.println(); listTemp.clear(); listTemp.add(list.remove(0)); i = 0; }else{ break; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String num = sc.next(); test(num); for(int t: listTemp) System.out.print(t); } } 运行结果如下
12345234568923456789 12345 23456 89 23456789