zoukankan      html  css  js  c++  java
  • 算法(第4版)Robert Sedgewick 刷题 第一章(1)

        /**
         * @Description 颠倒数组排列顺序
         * @author SEELE
         * @date 2017年8月17日 上午10:56:17
         * @action sortArr
         */
        public static void sortArr() {
            int[] b = new int[6];
            int[] a = { 1, 2, 3, 4, 5, 6, 7 };
            for (int i = 0; i < a.length / 2; i++) {
                int temp = a[a.length - 1 - i];
                a[a.length - 1 - i] = a[i];
                a[i] = temp;
            }
            System.out.println(Arrays.toString(a));
        }
    
        /**
         * @Description 判读一个数是否是素数
         * @author SEELE
         * @date 2017年8月17日 上午11:00:02
         * @action sushu
         */
        public static void sushu() {
            int N = 17;
            if (N < 2) {
                System.out.println("不是素数");
            }
            for (int i = 2; i * i <= N; i++) {
                if (N % i == 0) {
                    System.out.println("不是素数");
                    return;
                }
            }
            System.out.println("是素数");
        }
    
        /**
         * @Description 计算平方根,牛顿迭代法
         * @author SEELE
         * @date 2017年8月17日 上午11:16:17
         * @action sqrt
         * @param c
         * @return
         */
        public static double sqrt(double c) {
            if (c < 0) {
                return Double.NaN;
            }
            double err = 1e-15;
            double t = c;
            while (Math.abs(t - c / t) > err * t) {
                t = (c / t + t) / 2.0;
            }
            return t;
        }
    
        /**
         * @Description (自写)二分查找,先做一个从小到大的数组排序
         * @author SEELE
         * @date 2017年8月17日 下午2:33:52
         * @action erfenfind
         */
        public static void erfenfind() {
            int[] a = { 54, 54, 56, 56, 78, 8, 3232, 56, 546, 546, 46, 7854, 12, 3255, 58, 678, 585, 23, 45, 3, 6, 8, 89,
                    6 };
            System.out.println(Arrays.toString(a));
            int find = 8;
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
            int lo = 0;
            int hi = a.length - 1;
            while (lo <= hi) {
                int mid = lo + (hi - lo) / 2;
                if (find > a[mid]) {
                    lo = mid + 1;
                } else if (find < a[mid]) {
                    hi = mid - 1;
                } else {
                    System.out.println(mid + "---" + a[mid]);
                    break;
                }
            }
        }
    
        
        /**
         * 将一个正整数的转换成二进制,并已字符串打印出来
         */
        public static void binaryString() {
            long N = 5646753274687L;
            String s = "";
            for (long n = N; n > 0; n /= 2)
                s = (n % 2) + s;
            System.out.println(s);
        }
        
        /**
         * 1.1.13 编写一段代码,打印出一个M 行N 列的二维数组的转置(交换行和列)。
         */
        public static void MNtoNM() {
            int b = 0;
            int m = 10;
            int n = 3;
            int[][] a = new int[m][n];
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[i].length; j++) {
                    a[i][j] = b;
                    b++;
                }
            }
            for (int[] is : a) {
                for (int i : is) {
                    System.out.print(i+" ");
                }
                System.out.println();
            }
            int[][] c = new int[n][m];
            for(int i=0;i<n;i++)  
            {  
                for(int j=0;j<m;j++)  
                {  
                    c[i][j] = a[j][i];  
                }  
            }  
            System.out.println("-----------分割------------");  
            for (int[] is : c) {
                for (int i : is) {
                    System.out.print(i+" ");
                }
                System.out.println();
            }
        }
  • 相关阅读:
    P站画师 GTZ taejune 精选4k插画壁纸
    点、向量与坐标系
    一些几何
    画直线算法 Line drawing algorithm
    DX11 学习大纲
    插值 Interpolation
    The History of Computer Graphics
    vue中的请求拦截响应
    Event loop
    小程序使用wx.navigateTo()跳转失败
  • 原文地址:https://www.cnblogs.com/wzk1992/p/7382981.html
Copyright © 2011-2022 走看看