几道有趣的算法题

   题目:给定一个数字,得出这个数字的连续正整数之和,如给出15,得出1+2+3+4+5=4+5+6=7+8



1
public static void main(String[] args) { 2 ArrayList<ArrayList<Integer>> integers= new ArrayList<>(); 3 //假定给给出的数子是15 4 Integer num = 15; 5 6 /** 7 * 循环调用method方法,其实第14行代码的调用可以放到37行代码下面, 8 * 但是这样就回出现同一个集合重复的情况, 9 * 我的解决办法是用HashMap<String,ArrayList<Integer>>进行存储, 10 * 连续正整数开始的数字和结尾的数字拼接成 字符串作为key,在存储是进行判断,map中没有再存储 11 * 但是我觉得这样的话就有点过于复杂,我就没有那样写了 12 */ 13 for (int i = 1; i < num; i++) { 14 method(num, i, 0, integers); 15 } 16 //遍历输出    17 for (ArrayList<Integer> integer:integers) { 18 for (Integer i:integer) { 19 System.out.print(i); 20 } 21 System.out.println(""); 22 } 23 } 24 25 /** 26 * 27 * @param num 给定的数字 28 * @param start 连续正整数开始的数字 29 * @param current 当前的数字 30 * @param integers 用来答案的集合 31 */ 32 public static void method(Integer num, Integer start, Integer current, ArrayList<ArrayList<Integer>> integers) { 33 34 if (count(start, current) > num) { 35 return; 36 }else if (count(start,current)< num) { 37 method(num, start, ++current, integers); 38 } else if (count(start, current) == num) { 39 integers.add(util(start, current)); 40 } 41 } 42 43 /** 44 * 计算答案,用来判断这一组连续正整数是否等于给的数 45 * @param start 46 * @param end 47 * @return 48 */ 49 public static Integer count(int start,int end){ 50 int sum = 0; 51 for (int i = start; i <= end; i++) { 52 sum += i; 53 } 54 return sum; 55 } 56 57 /** 58 * 将可以组成答案集合的连续正整数的开始和结尾的数字变成答案集合 59 * @param start 60 * @param end 61 * @return 62 */ 63 public static ArrayList<Integer> util(Integer start, Integer end) { 64 ArrayList<Integer> integers = new ArrayList<>(); 65 for (int i = start; i <= end; i++) { 66 integers.add(i); 67 } 68 return integers; 69 }

 运行结果

第二题

 1 /**
 2  * 给出一个数组,将这个数组的奇数放在数组的前半部分,偶数放在后半部分
 3  */
 4 public class two {
 5 
 6     public static void main(String[] args) {
 7         /**
 8          * 假定我给出的数组如下
 9          */
10         Integer[] integers = {1,2,3,4,5,6,7,8,9};
11         //调用方法
12         method(0, 0, integers);
13         //打印结果
14         for (Integer integer:integers) {
15             System.out.printf(String.valueOf(integer));
16         }
17     }
18 
19     /**
20      *
21      * @param current 当前运算的下标
22      * @param count 运算了多少次,用来防止死循环
23      * @param integers  运算的数组
24      * @return
25      */
26     public static Integer[] method(int current,int count,Integer[] integers) {
27         if (count >= integers.length) {
28             return integers;
29         }
30 
31         //如果当前运算的是数字是奇数,那么就不管他,继续下一个
32         if (integers[current] % 2==1) {
33             current++;
34         } else {
35             //是偶数就换到数组最后,然后数组从当前下标之后的下标往前移动一位
36             swep(current, integers);
37         }
38         //迭代
39         return method(current, ++count, integers);
40     }
41 
42 
43     /**
44      * 把这个数放到最后,并且原来所有的数往前面以一个位置
45      * @param current
46      * @param array
47      */
48     public static void swep(int current,Integer[] array) {
49         int currentvalue = array[current];
50         for (int i = current; i < array.length - 1; i++) {
51             array[i] = array[i + 1];
52         }
53         array[array.length - 1] = currentvalue;
54     }
55 }

 运行结果:

 

 这篇博客有序还会保持更新,如有兴趣,请保持关注

当然如果你也有一些有意思的算法题,请联系我 vx:18569022155

如果你有更好的解法请务必联系我,我的邮箱2310874577@qq.com,多谢

posted @ 2019-05-29 08:39  刘阿泽  阅读(289)  评论(0编辑  收藏  举报