zoukankan      html  css  js  c++  java
  • 水仙花,排序等

    /**
     * 求1000以内的所有水仙花数.它的各位数字的立方相加等于这个数本身.。
     */
    public class NumberOfDaffodils {
       public static void main(String[] arg0) {
            for(int i=100;i<=999;i++){
                 int a,b,c;
                a=i / 100;   //  符号 /是求余   % 是求商
                b=i % 100 / 10;
                c=i % 10;
                if(i==a*a*a+b*b*b+c*c*c){
                    System.out.println(i);
                }
            }
    
        }
    }

    输出结果:

     1 /**
     2  * 冒泡排序
     3  */
     4 public class BubbleSort {
     5    public static void main(String[] arg0) {
     6         int[] arr ={6,3,8,2,9,1};
     7        System.out.println("排序前:");
     8         for(int i:arr){
     9             System.out.print(i+",");
    10         }
    11 
    12         for(int i=0;i<arr.length-1;i++){ //外层控制的是:循环的趟数
    13             for(int j=0;j<arr.length-1-i;j++){ //内层控制的是: 每趟比较了几次
    14                 if(arr[j]>arr[j+1]){
    15                     int temp=arr[j];
    16                     arr[j]=arr[j+1];
    17                     arr[j+1]=temp;
    18                 }
    19             }
    20         }
    21        System.out.println("排序后:");
    22        for (int i : arr) {
    23            System.out.print(i+",");
    24        }
    25     }
    26 }

    输出结果:

    选择排序:

     1 /**
     2  * 选择排序. 每次寻找最小(或最大)的数,放在头或者未.
     3  */
     4 public class selectionSort {
     5    public static void main(String[] arg0) {
     6         int[] arr ={6,3,8,2,9,1};
     7        System.out.println("排序前:");
     8         for(int i:arr){
     9             System.out.print(i+",");
    10         }
    11 
    12         for(int i=0;i<arr.length-1;i++){ //外层控制的是:循环的趟数
    13             int minIndex=i;   //从第i个开始比较,寻找最小的数.
    14             for(int j=i+1;j<arr.length;j++){ //i的下一个和i进行比较
    15                 if(arr[j]<arr[minIndex]){
    16                     minIndex = j;  //记录最小下标
    17                 }
    18             }
    19             int temp=arr[i];
    20             arr[i]=arr[minIndex];
    21             arr[minIndex]=temp;
    22         }
    23        System.out.println("排序后:");
    24        for (int i : arr) {
    25            System.out.print(i+",");
    26        }
    27     }
    28 }

     插入排序:

    动态图片地址: https://images2017.cnblogs.com/blog/849589/201710/849589-20171015225645277-1151100000.gif

     1 /**
     2  * 从第一个元素开始,该元素可以认为已经被排序;
     3  * 取出下一个元素,在已经排序的元素序列中从后向前扫描;
     4  * 如果该元素(已排序)大于新元素,将该元素移到下一位置;
     5  * 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
     6  * 将新元素插入到该位置后;
     7  * 重复步骤2~5。
     8  */
     9 public class selectionSort {
    10    public static void main(String[] arg0) {
    11         int[] arr ={6,3,8,2,9,1};
    12        System.out.println("排序前:");
    13         for(int i:arr){
    14             System.out.print(i+",");
    15         }
    16 
    17         for(int i=1;i<arr.length;i++){
    18            int preIndex=i-1;
    19            int temp = arr[i];
    20             while(preIndex>=0 && arr[preIndex]>temp){
    21                 arr[preIndex+1]=arr[preIndex];
    22                 preIndex--;
    23             }
    24             arr[preIndex + 1]=temp; //因为上面--,所以现在preIndex+1就是i-1
    25         }
    26        System.out.println("排序后:");
    27        for(int i : arr) {
    28            System.out.print(i+",");
    29        }
    30     }
    31 }
  • 相关阅读:
    MongoDB
    Django配置实现数据库读写分离
    基于scrapy-redis的分布式爬虫
    增量式爬虫
    Pyhton网络爬虫之CrawlSpider
    Scrapy 之如何发送post请求
    Scrapy 之settings配置
    Scrapy 实现爬取多页数据 + 多层url数据爬取
    Scrapy 框架入门简介
    redis操作总结
  • 原文地址:https://www.cnblogs.com/bulrush/p/10573134.html
Copyright © 2011-2022 走看看