0
点赞
收藏
分享

微信扫一扫

2021404209陈聪01

夏侯居坤叶叔尘 2022-03-11 阅读 52
java

#java代码

#####1.HelloWorld

public class HelloWorld
{
	public static void main(String [] args)
	{
		System.out.println("Hello World!");
	}
}

#####2.数据类型

public class Test01
{
	public static void main(String [] args)
	{
		byte t = (byte)128;
		System.out.println(t);

		byte b = -127;
		System.out.println(b);

		b -= 1;
		System.out.println(b);

		long a = 1000L;
		System.out.println(a);

		float m = 9.18880f; //后面要加f
		System.out.println(m);
		int p = (int)m;
		System.out.println(p);

		char c = (char)p; //p为整型
		System.out.println(c);

		boolean f = true;
		System.out.println(f);
		//int ff = (int)f;不能转换
	}
}

#####3. 逻辑运算符

public class Test02 {
    public static void main(String[] args) {
        
        // &与&& |与|| 的区别
        // && || 有短路效果 , &,|没有,照样会执行后面的语句
        int a = 1 , b = 2 ;

        if(a > b && b == ++a);
        System.out.println(a);// a = 1;

        a = 1 ; b = 2;
        if(a > b & b == ++a);
        System.out.println(a);// a = 2;

        // | 与 || 同理
    }
}

#####4. 输出九九乘法表

public class MultiplicationTable {
    public static void main(String[] args) {
        for(int i = 1 ; i <= 9 ; i ++)
        {
            for(int j = 1 ; j <= i ; j++)
            {
                System.out.format("%d*%d = %d   ",i,j,i*j);
            }
            System.out.println();
        }
    }
    
}
/*
结果
1*1 = 1   
2*1 = 2   2*2 = 4
3*1 = 3   3*2 = 6   3*3 = 9
4*1 = 4   4*2 = 8   4*3 = 12   4*4 = 16
5*1 = 5   5*2 = 10   5*3 = 15   5*4 = 20   5*5 = 25
6*1 = 6   6*2 = 12   6*3 = 18   6*4 = 24   6*5 = 30   6*6 = 36
7*1 = 7   7*2 = 14   7*3 = 21   7*4 = 28   7*5 = 35   7*6 = 42   7*7 = 49
8*1 = 8   8*2 = 16   8*3 = 24   8*4 = 32   8*5 = 40   8*6 = 48   8*7 = 56   8*8 = 64
9*1 = 9   9*2 = 18   9*3 = 27   9*4 = 36   9*5 = 45   9*6 = 54   9*7 = 63   9*8 = 72   9*9 = 81
*/

#####5. Java版冒泡排序

import java.util.Scanner;
public class Sort {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();//输入n
        int [] arr = new int[n + 1];
        for(int i = 0 ; i < n ; i ++)
            arr[i] = in.nextInt();//输入n个数据

        for(int i = 0 ; i < n ; i ++)//冒泡
        {
            for(int j = i ; j < n ; j ++)
            {
                if(arr[i] > arr[j] )
                  {
                      int t; t= arr[i]; arr[i] = arr[j]; arr[j] = t;
                  }
            }
        }

        for(int i = 0 ; i < n ; i ++)
            System.out.print(arr[i]+" ");
    }
}

#####6.Java版快排(对n个数排序)

import java.util.*;

public class QuickSort {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);//创建Scanner类读入数据

        int n = in.nextInt();//输入n

        int [] arr = new int[n + 5];//创建数组

        for(int i = 0 ; i < n ; i ++)
            arr[i] = in.nextInt();//输入数据

        quickSort(arr , 0 , n - 1);//快排

        for(int i = 0 ; i < n ; i++) 
			System.out.print(arr[i]+ " ");//输出

    }  
	//快排思想:1.在数组中选取一个基数x,将比x小的数放在x的左边,大的放右边
	//2. 然后在递归重复执行,直至完成排序
    //难点在于怎么执行第一步,我们可以在数组左右两边设置两个指针,不断向右向左寻找不满足条件的数,然后交换两个数,就完成第一步了
    public static void quickSort(int [] arr ,int l , int r)
    {
        if(l >= r) return ; //递归跳出条件

		//设置基数,左右指针
        int x = arr[(l + r) / 2] , i = l - 1 ,  j = r + 1; 

        while(i < j)//完成第一步
        {
            do i ++ ;while(x > arr[i]);
            do j -- ;while(x < arr[j]);
            if(i < j) 
			{
                int t; t = arr[i]; arr[i] = arr[j]; arr[j] = t;
            }
        }

        quickSort(arr , l , j);//递归左右两边
        quickSort(arr , j + 1 , r);
    }
    
}


举报

相关推荐

0 条评论