1、输出杨辉三角
package 暑假2022;
import java.util.*;
public class 杨辉三角 {
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner sc=new Scanner(System.in) ;
int [][] m=new int[10][10];
int i=0;
int j=0;
for(;i
m[i][j]=1;
}else {
m[i][j]=m[i-1][j-1]+m[i-1][j];
}
System.out.print(m[i][j]+"t");
}
System.out.println();
}
}
}
2、冒泡排序
package 暑假2022;
import java.util.*;
public class 冒泡 {
public static void main(String[] args) { // TODO Auto-generated method stub int[] arr={6,3,8,2,9,1}; System.out.println(Arrays.toString(arr)); for (int i=0;iarr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println(Arrays.toString(arr)); } }
3、选择排序
package 暑假2022;
import java.util.Arrays;
public class 选择 {
public static void main(String[] args) { // TODO Auto-generated method stub
int[] a= {2,34,556,6767,87,54,56,1};
for(int j=0;j
int index=j;
for(int i=1+index;i
if(a[i]
index=i;
}
}
int t=a[j];
a[j]=a[index];
a[index]=t;
System.out.println(Arrays.toString(a));
}
}
}
4、插入排序
package 暑假2022;
public class 插入 {
public static void insertSort(int[] arr) {
if (arr == null || arr.length == 0) return;
for (int i = 0; i < arr.length - 1; i++) {
int value = arr[i + 1];
int j = i;
for (; j >= 0 && arr[j] > value; j–) {
arr[j + 1] = arr[j];
}
arr[j + 1] = value;
System.out.print("第 "+ (i+1) + " 趟: ");
print(arr);
}
}
public static void main(String[] args) {
int[] arr = {6, 9, 1, 4, 5, 8, 7, 0, 2, 3};
System.out.print("排序前: ");
print(arr);
insertSort(arr);
System.out.print("排序后: "); print(arr); } public static void print(int[] arr) { if (arr == null) return; for(int i : arr) { System.out.print(i + " "); } System.out.println(); } }
5、创建要给长度为6的int类型数组,要求数组元素的值再1-30之间,且随机赋值。同时要求数组元素各不相同
package 暑假2022;
import java.util.*;
public class 数组2 {
public static void main(String[] args) { // TODO Auto-generated method stub boolean f=true; int[] d= new int[6]; int index=0; Random num=new Random();
while (true) {
int h=num.nextInt(30)+1;
for(int i=0;i<=index;i++) {
if(h==d[i]) {
f=false;
break;
}
}
if(f) {
d[index]=h;
index++;
if(index==6) {
break;
}
}
}
System.out.println(Arrays.toString(d));
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)