算法学习

因为我高中数学总是考的无限接近满分,山东理科数学哦,所以我一直对算法比较感兴趣,却没时间去操作。最近很长时间感觉自己智商捉急,直线下滑,所以就开始研究这个了。碰到的算法题都会记录在下,有更优解请直接打脸。

  • 1.海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

    public class Monkey {
        /**
         * 思路: 每次拿掉一个桃子之后,剩下的一定是5的倍数,反复五次
         */
        boolean getNo(int sum) {
    
            for (int i = 0; i < 5; i++) {
                if ((sum - 1) % 5 == 0) {
                    sum = (sum - 1) * 4 / 5;
                } else {
                    return false;
                }
            }
            return true;
        }
    
        public static void main(String[] args) {
            Monkey t = new Monkey();
            int i = 1;
            while (true) {
                if (t.getNo(i)) {
                    break;
                }
                i++;
            }
            System.out.println(i);
    
        }
    }
    
  • 2.括号配对问题:数据保证S中只含有[,],(,)四种字符,每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No

    思路:把字符串丢进堆栈里面,如果下一个字符和上一个匹配,就把栈顶的拿出来。

    
    public class ACMModel {
    
        final static String a = "[[()]]";
    
        static String ml = "[";
        static String mr = "]";
        static String sl = "(";
        static String sr = ")";
    
        public static void main(String[] args) {
    
            int size = a.length();
    
            Stack stack = new Stack<>();
            if (String.valueOf(a.charAt(0)).equals(mr) ||
                    String.valueOf(a.charAt(0)).equals(sr)) {
                System.out.println("no");
            } else {
                for (int i = 0; i < size; i++) {
                    String temp = String.valueOf(a.charAt(i));
                    if (stack.empty()) {
                        stack.push(a.charAt(i));
                    } else {
                        if (String.valueOf(stack.peek()).equals(ml) && 
                        temp.equals(mr){
                            stack.pop();
                        } else if (String.valueOf(stack.peek()).equals(sl) &&                                       temp.equals(sr)) {
                            stack.pop();
                        } else {
                            stack.push(a.charAt(i));
                        }
    
                    }
                }
                if (!stack.empty()) {
                    System.out.println("no");
                } else {
                    System.out.println("yes");
                }
            }
        }
    }
    
  • 3.计算数组中单词出现的次数

    public static void main(String[] args) {
            String[] array = {"a", "b", "c", "a", "b", "a", "a"};
            System.out.println("method1:" + methodOne(array).toString());
        }
    
        static HashMap methodOne(String[] array) {
            HashMap map = new HashMap<>();
            for (int i = 0; i < array.length; i++) {
                Integer count = map.get(array[i]);
                if (count == null) {
                    map.put(array[i], 1);
                } else {
                    map.put(array[i], count + 1);
                }
            }
        }
    
  • 4.100个男性朋友围成一个圈,从1号开始数,数到第3个人就out,最后留下来的人把女神许配给他。
    问:哪个位置才是幸运儿?

思路:双向链表把数据挨个丢进去,并把最后一个值指向第一个值。遇到满足条件的就删除。最后节点的下一个和当前是同一个的时候就结束。(answer:91)

```

public static void main(String[] args) {

    int killSpace = 3;//从1开始但不包括1 第几个出圈
    int totalData = 100;//总共的参与人数
    Mode mode;
    Mode current = new Mode(1);
    mode = current;
    for (int i = 2; i <= totalData; i++) {
        current.next = new Mode(i);
        current.next.pre = current;
        current = current.next;
    }
   //mode.print();
    current.next = mode;
    mode.pre = current;

    int i = 1;
    //mode.pre.next = mode.next;
    //mode.next.pre = mode.pre;
   // mode = mode.next;

    while (mode != mode.next) {
        if (i % killSpace == 0) {
            System.out.println("delete ====" + mode.current);
            mode.pre.next = mode.next;
            mode.next.pre = mode.pre;
            mode = mode.pre.next;
        } else {
            mode = mode.next;
        }
        i++;
    }
    System.out.println(mode.current);
    System.out.println(mode.next);

}

static class Mode {
    int current;
    Mode next;
    Mode pre;

    Mode(int current) {
        this.current = current;
    }

    void print() {
        System.out.println(current);
        Mode pr = next;
        while (pr != null) {
            System.out.println(pr.current);
            if (pr == pr.next) break;
            pr = pr.next;
        }
    }
}
```

你可能感兴趣的:(算法学习)