博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
几道算法水题
阅读量:5037 次
发布时间:2019-06-12

本文共 5425 字,大约阅读时间需要 18 分钟。

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
arrayList; static{ arrayList = new ArrayList
(); } 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)); } }}

 

转载于:https://www.cnblogs.com/handsomecui/p/6019125.html

你可能感兴趣的文章
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>
http初探
查看>>
elasticsearch的安装
查看>>
__next__()
查看>>
爬取:中国大学排名
查看>>
聊天室(C++客户端+Pyhton服务器)_1.框架搭设
查看>>
UpdatePanel 内控件 更新“外的”控件【转】
查看>>
mybatis中&gt;=和&lt;=的实现方式
查看>>
Python面向对象03/继承
查看>>
java序列化和反序列化
查看>>
绝对定位
查看>>
flink源码编译(windows环境)
查看>>
dpkg 删除 百度网盘 程序
查看>>
服务器nginx安装
查看>>
std::nothrow
查看>>
rest-framework 分页器
查看>>
JQuery(一)安装&选择器 样式篇
查看>>
浏览器的DNS缓存查看和清除
查看>>
浏览器跨域问题
查看>>