几道算法水题
1.汉诺塔问题:
import java.util.Scanner; public class Hanoi { public static void main(String[] args) { int n; Scanner cin = new Scanner(System.in); while(cin.hasNext()){ n = cin.nextInt(); hanoi(n, 'A', 'C', 'B'); } } public static void hanoi(int n, char a, char b, char c){ if(n == 1){ move(a, b); return; } hanoi(n-1, a, c, b); move(a, b); hanoi(n-1, c, b, a); } public static void move(char a, char b){ System.out.println("move " + a + " to " + b); } }
2.归并排序:
import java.util.Scanner; public class MergeSort { public static int[] a, b; public static void main(String[] args) { int l; Scanner cin = new Scanner(System.in); while(cin.hasNext()){ l = cin.nextInt(); a = new int[l + 1]; b = new int[l + 1]; for(int i = 1; i <= l; i++){ a[i] = cin.nextInt(); } ms(1, l); for(int i = 1; i <= l; i++){ System.out.printf("%d ", a[i]); } System.out.println(); } } public static void mergeSort(int l, int m, int r){ int i = l, j = m + 1, k; k = l; while(i <= m && j <= r){ if(a[i] <= a[j]) b[k++] = a[i++]; else b[k++] = a[j++]; } while(i <= m){ b[k] = a[i]; k++; i++; } while(j <= r){ b[k] = a[j]; k++; j++; } for(k = l; k <= r; k++){ a[k] = b[k]; } } public static void ms(int l, int r){ if(l < r){ int m = (l + r) / 2; ms(l, m); ms(m+1, r); mergeSort(l, m, r); } } }
3.求众数:
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import sun.misc.Compare; import sun.misc.Sort; /** * @author Administrator * */ class Num{ public int value; public int count; public Num() { value = 0; count = 0; } public Num(int value, int count) { super(); this.value = value; this.count = count; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public String toString() { return "value = " + value + "\tcount = " + count; } } public class Zhongshu { static int[] num; static Num[] ans; static{ ans = new Num[10010]; for(int i = 0; i < 10010; i++){ ans[i] = new Num(); } } public static void main(String[] args) { int N; Scanner cin = new Scanner(System.in); while(cin.hasNext()){ N = cin.nextInt(); num = new int[N]; for(int i = 0; i < N; i++){ num[i] = cin.nextInt(); } Arrays.sort(num); int value = num[0], last = 0, count, k = 0; for(int i = 1; i < N; i++){ if(value != num[i]){ count = i - last; ans[k].setCount(count); ans[k].setValue(value); k++; last = i; value = num[i]; } } count = N - last; ans[k].setCount(count); ans[k].setValue(value); k++; // for(int i = 0; i < k; i++){ // System.out.println(ans[i]); // } Arrays.sort(ans, 0, k, new MyComparator()); for(int i = 0; i < k; i++){ System.out.println(ans[i]); } } } } class MyComparator implements Comparator{ public int compare(Object a, Object b) { if(a == null || b == null){ System.out.println("无奈啊!!!"); } Num o1 = (Num)a; Num o2 = (Num)b; if(o1.getCount() != o2.getCount()) return o1.getCount() - o2.getCount(); else return o1.getValue() - o2.getValue(); } }
4.约瑟夫环:
import java.util.ArrayList; import java.util.Scanner; public class 约瑟夫环 { public static ArrayList<Integer>arrayList; static{ arrayList = new ArrayList<Integer>(); } static void init(int n){ arrayList.clear(); for(int i = 1; i <= n; i++){ arrayList.add(i); } } static int work(int ele, int pos){ if(arrayList.size() == 1) return arrayList.get(0); else{ int cur = (pos + ele - 1)%arrayList.size(); if(cur == 0) cur = arrayList.size(); System.out.printf("%d死了\n", arrayList.get(cur - 1)); arrayList.remove(cur - 1); //System.out.println(arrayList.size()); return work(ele, cur%arrayList.size() == 0? arrayList.size() : cur%arrayList.size()); } } public static void main(String[] args) { Scanner cin = new Scanner(System.in); int m, n; while(cin.hasNext()){ n = cin.nextInt(); m = cin.nextInt(); init(n); System.out.printf("最后%d活着\n", work(m, 1)); } } }