请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。
输入数据:一个正整数,以命令行参数的形式提供给程序。
输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、以从小到大的顺序打印。如果结果有多个序列,按各序列的最小正整数的大小从小到大打印各序列。此外,序列不允许重复,序列内的整数用一个空格分隔。如果没有符合要求的序列,输出“NONE”。
例如,对于15,其输d出结果是:
1 2 3 4 5
4 5 6
7 8
对于16,其输出结果是:
NONE
下面是我的实现
1 public class Test { 2 3 public static void main(String[] args) { 4 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 5 Integer input; 6 try { 7 input = Integer.parseInt(reader.readLine()); 8 if(input%2==0){ 9 System.out.println("NONE"); 10 }else{ 11 Integer half = input/2; 12 for(int i = 1;i<=half;i++){ 13 int result = 0; 14 List<Integer> results = new ArrayList<Integer>(); 15 for(int j = i;j<=half;j++){ 16 result+=j; 17 results.add(new Integer(j)); 18 if(result==input){ 19 break; 20 }else if(result>input){ 21 results.clear(); 22 } 23 } 24 if(result<input){ 25 results.clear(); 26 } 27 for(Integer aa:results){ 28 System.out.print(aa+" "); 29 if(results.get(results.size()-1)==aa){ 30 System.out.println(); 31 } 32 } 33 } 34 int next = half +1; 35 System.out.println(half+" "+next); 36 } 37 38 } catch (NumberFormatException e) { 39 e.printStackTrace(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } 43 } 44 }