因?yàn)槲腋咧袛?shù)學(xué)總是考的無(wú)限接近滿分,山東理科數(shù)學(xué)哦他宛,所以我一直對(duì)算法比較感興趣,卻沒(méi)時(shí)間去操作欠气。最近很長(zhǎng)時(shí)間感覺(jué)自己智商捉急厅各,直線下滑,所以就開(kāi)始研究這個(gè)了预柒。碰到的算法題都會(huì)記錄在下队塘,有更優(yōu)解請(qǐng)直接打臉。
-
1.海灘上有一堆桃子宜鸯,五只猴子來(lái)分憔古。第一只猴子把這堆桃子憑據(jù)分為五份,多了一個(gè)顾翼,這只猴子把多的一個(gè)扔入海中投放,拿走了一份。第二只猴子把剩下的桃子又平均分成五份适贸,又多了一個(gè)灸芳,它同樣把多的一個(gè)扔入海中涝桅,拿走了一份,第三烙样、第四冯遂、第五只猴子都是這樣做的,問(wèn)海灘上原來(lái)最少有多少個(gè)桃子谒获?
public class Monkey { /** * 思路: 每次拿掉一個(gè)桃子之后蛤肌,剩下的一定是5的倍數(shù),反復(fù)五次 */ 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.括號(hào)配對(duì)問(wèn)題:數(shù)據(jù)保證S中只含有
[
,]
,(
,)
四種字符批狱,每組輸入數(shù)據(jù)的輸出占一行裸准,如果該字符串中所含的括號(hào)是配對(duì)的,則輸出Yes,如果不配對(duì)則輸出No思路:把字符串丟進(jìn)堆棧里面赔硫,如果下一個(gè)字符和上一個(gè)匹配炒俱,就把棧頂?shù)哪贸鰜?lái)。
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<Character> 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.計(jì)算數(shù)組中單詞出現(xiàn)的次數(shù)
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<String, Integer> 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個(gè)男性朋友圍成一個(gè)圈爪膊,從1號(hào)開(kāi)始數(shù)权悟,數(shù)到第3個(gè)人就out,最后留下來(lái)的人把女神許配給他推盛。
問(wèn):哪個(gè)位置才是幸運(yùn)兒峦阁?
思路:雙向鏈表把數(shù)據(jù)挨個(gè)丟進(jìn)去,并把最后一個(gè)值指向第一個(gè)值耘成。遇到滿足條件的就刪除榔昔。最后節(jié)點(diǎn)的下一個(gè)和當(dāng)前是同一個(gè)的時(shí)候就結(jié)束。(answer:91)
```
public static void main(String[] args) {
int killSpace = 3;//從1開(kāi)始但不包括1 第幾個(gè)出圈
int totalData = 100;//總共的參與人數(shù)
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;
}
}
}
```