码迷,mamicode.com
首页 > 编程语言 > 详细

java小程序整理及排序算法

时间:2016-10-24 02:09:01      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:技术分享   []   它的   bin   print   count   tag   auto   gravity   

1. 利用循环打印如下图形

*****
****
***
**
*
public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 5;
        while (i >= 0) {
            for (int j = i; j > 0; j--)
                System.out.print("*");
            System.out.println();
            i--;
        }
    }
 
}

2.利用循环打印如下图形

*
**
***
****
*****
****
***
**
*
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int i = 1;
        while (i <= 5) {
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.println();
            i++;
        }
        i = 4;
        while (i >= 0) {
            for (int j = i; j > 0; j--)
                System.out.print("*");
            System.out.println();
            i--;
        }
    }
 
}

3.利用循环打印如下图形

 技术分享
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int k = 5;
        for (int i = 1; i <= k; i++) {
            for (int m = k - i; m > 0; m--)
                System.out.print(" ");
            for (int n = i; n > 0; n--)
                System.out.print("* ");
            System.out.println();
        }
        for (int i = k - 1; i >= 0; i--) {
            for (int m = 1; m <= k - i; m++)
                System.out.print(" ");
            for (int n = i; n > 0; n--)
                System.out.print("* ");
            System.out.println();
        }
    }
 
}

4.输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。

例如: 153 = 1*1*1 + 3*3*3 + 5*5*5
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 9; i++)
            for (int j = 0; j <= 9; j++)
                for (int k = 0; k <= 9; k++) {
                    if (i * i * i + j * j * j + k * k * k == (i * 100 + j * 10 + k))
                        System.out.println(i * 100 + j * 10 + k);
                }
    }
}

5.

问题一:

从键盘读入个数为10个的整数,并判断读入的正数和负数的个数。
问题二:
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。

 
import java.util.Scanner;
 
 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        funCount(sc.nextInt());
        sc.close();
    }
    private static void funCount(int type) {
        Scanner sc=new Scanner(System.in);
        int i=0,plus=0,minus=0;
        if(type==1){
            System.out.println("输入10个数");
            do{
               if(sc.nextInt()>=0)plus++;
               else
                   minus++;
               i++;
            }while(i<10);
            System.out.println("正数的个数为:"+plus+"负数的个数为:"+minus);
        }
        else{
            System.out.println("输入任意个数字,以0结束");
            int ip;
            while((ip=sc.nextInt())!=0){
                if(ip>0)plus++;
                else
                    minus++;
            }
            System.out.println("正数的个数为:"+plus+"负数的个数为:"+minus);
        }
        sc.close();
    }
 
}

6.100以内的所有质数

 
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 2; i <= 100; i++) {
            int sum = 0;
            if (i == 2 || i == 3)
                System.out.println(i);
            else {
                for (int j = 2; j < i; j++) {
                    if (i % j == 0) {
                        sum++;
                        break;
                    }
                }
                if (sum == 0)
                    System.out.println(i);
            }
        }
    }
}

7.一个数如果恰好等于它的因子之和,这个数就称为"完数"。(因子:除去这个数本身正的约数)
例如6=1+2+3.编程 找出1000以内的所有完数

public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 1000; i++) {
            int sum = 0;
            for (int j = 1; j <= i / 2; j++) {
                if (i % j == 0)
                    sum += j;
            }
            if (sum == i)
                System.out.println(i);
        }
    }
}

【以下可能用到数组,把排序的数可以提前初始化到一个int数组当中,如
int[] dataSort = new int[]{9, -16, 21, 23, -30, -49, 21, 30, 30}; 
访问其中每个数就利用下标进行,下标从0开始的,如  取dataSort[2],得到的值就是 21.

以下是把数据结构当中所学的基本排序算法都一一实现一下。
8.直接插入排序  10. 直接选择排序  11. 冒泡排序  12. Shell排序  13. 快速排序
第9-13题的排序每个同学们必做,14-17同学们根据自己的能力选做
【14. 折半插入排序  15. 堆排序   16. 归并排序   17.基数排序 】

 
注:这里采用随机数组,取值范围是在-100到100之间,每个排序方法定义一个类,保证使用方式上基本一致,后面三种排序方法写起来麻烦懒得写了。
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int dataSort[] = new int[10];
        for (int i = 0; i < dataSort.length; i++) {
            dataSort[i] = (int) (Math.random() * 200) - 100;
        }
        int dataBackup[] = dataSort;// 备份数组保证每次排序的初始内容一样
 
        dataSort = dataBackup;
        StraightInsertionSort sis = new StraightInsertionSort(dataSort);// 直接插入排序
        printData(sis.TAG, sis.data);
 
        dataSort = dataBackup;
        StraightSelectSort sss = new StraightSelectSort(dataSort);// 直接选择排序
        printData(sss.TAG, sss.data);
 
        dataSort = dataBackup;
        BubbleSort bbs = new BubbleSort(dataSort);// 冒泡排序
        printData(bbs.TAG, bbs.data);
 
        dataSort = dataBackup;
        ShellSort ss = new ShellSort(dataSort);// Shell排序
        printData(ss.TAG, ss.data);
 
        dataSort = dataBackup;
        QuickSort qs = new QuickSort(dataSort);
        printData(qs.TAG, qs.data);
 
        dataSort = dataBackup;
        BinarySort bs = new BinarySort(dataSort);// 折半插入排序
        printData(bs.TAG, bs.data);
    }
 
    private static void printData(String a, int[] d) {
        // TODO Auto-generated method stub
        System.out.printf("%-15s\t", new Object[] { a });
        System.out.print("-->\t");
        for (int i = 0; i < d.length; i++) {
            System.out.print(d[i] + "\t");
        }
        System.out.println();
    }
}
 
public class StraightInsertionSort {
    String TAG="直接插入排序";
    int data[];
    public StraightInsertionSort(int data[]){
        this.data=data;
        sort(data);
    }
    private void sort(int[] d) {
        // TODO Auto-generated method stub
        int temp;// 存放最小元素
        for (int i = 1; i < d.length; i++) {
            int j = i - 1;
            temp = d[i];
            while (temp < d[j]) {
                d[j+1] = d[j];
                j--;
                if (j < 0)
                    break;
            }
            d[j + 1] = temp;
        }
    }
}
 
public class StraightSelectSort {
    String TAG="直接选择排序";
    int data[];
    public StraightSelectSort(int data[]){
        this.data=data;
        sort(data);
    }
    private void sort(int[] d) {
        // TODO Auto-generated method stub
        int temp;// 临时变量
        for (int i = 1; i < d.length; i++) {
            int k = i;// 记录最小元素位置
            for (int j = i + 1; j < d.length; j++) {
                if (d[j] < d[k])
                    k = j;
                if (k != i) {
                    temp = d[k];
                    d[k] = d[i];
                    d[i] = temp;
                }
            }
        }
    }
}
 
public class BubbleSort {
    String TAG = "冒泡排序";
    int data[];
 
    public BubbleSort(int data[]) {
        this.data = data;
        sort(data);
    }
 
    private void sort(int[] d) {
        // TODO Auto-generated method stub
        boolean hasSort = true;// 判断是否进行排序了的标识
        int i = 0;
        while (i < d.length && hasSort == true) {
            hasSort = false;
            for (int j = 0; j < d.length - 1; j++) {
                if (d[j + 1] < d[j]) {
                    int temp = d[j];
                    d[j] = d[j + 1];
                    d[j + 1] = temp;
                    hasSort = true;
                }
            }
            i++;
        }
    }
}
 
public class ShellSort {
    String TAG="Shell排序";
    int data[];
    public ShellSort(int data[]){
        this.data=data;
        sort(data);
    }
    private void sort(int[] d) {
        // TODO Auto-generated method stub
        for (int t = d.length / 2; t >= 1; t = t / 2) {
            for (int i = t ; i < d.length; i++) {
                int temp = d[i];
                int j;
                for (j = i - t; j >= 0 && temp < d[j]; j = j - t) {
                    d[j + t] = d[j];
                }
                d[j + t] = temp;
            }
        }
    }
}
 
public class QuickSort {
    String TAG="快速排序";
    int data[];
    public QuickSort(int data[]){
        this.data=data;
        sort(data,0,data.length-1);
    }
    private void sort(int[] d, int left, int right) {
        // TODO Auto-generated method stub
        if(left>=right)return;
        int temp=0;
        temp=d[left];
        int l= left, r = right;
        while (l < r) {
            while (l<r&&d[r] > temp)
                r--;
            if(l<r)d[l++]=d[r];
            while (l<r&&d[l] < temp)
                l++;
            if(l<r)d[r--]=d[l];
        }
        d[l]=temp;
        sort(d, left, l-1);
        sort(d, l+1, right);
    }
}
public class BinarySort {
    String TAG = "折半插入排序";
    int data[];
 
    public BinarySort(int data[]) {
        this.data = data;
        sort(data);
    }
 
    private void sort(int[] d) {
        // TODO Auto-generated method stub
        for (int i = 1; i < d.length; i++) {
            int temp = d[i];
            int left = 0, right = i - 1;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (d[i] < d[mid])
                    right = mid - 1;
                else
                    left = mid + 1;
            }
            for (int j = i - 1; j >= left; j--)
                d[j + 1] = d[j];
            d[left] = temp;
        }
    }
}

 

java小程序整理及排序算法

标签:技术分享   []   它的   bin   print   count   tag   auto   gravity   

原文地址:http://www.cnblogs.com/zjfjava/p/5991593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!