1.菱形
public static void main(String[] args) { for(int i=1;i<5;i++){ for(int j=1;j<5-i;j++){ System.out.print(" "); } for(int k=1;k<2*i;k++){ System.out.print("*"); } System.out.print("\n"); } for(int p=1;p<4;p++){ for(int q=1;q<p+1;q++){ System.out.print(" "); } for(int m=1;m<2*(4-p);m++){ System.out.print("*"); } System.out.print("\n"); } }
2.求鞍点:算法一:
/*找出一个二维数组的鞍点。 鞍点指某数组元素的值在该行上最大、在列上最小。也可能没有鞍点*/ public class andian { public static void main(String[] args) { int a[][]={{13,9,16},{5,4,3},{6,1,11}}; isAndian(a); } static void isAndian(int a[][]){ int row=a.length;//row int col=a[0].length; for(int i=0;i<row;i++){ int k=findMax(a,i);//行上最大返回列 int q=findMin(a,k);//列上最小返回行 if(q==i) { System.out.println("鞍点的行下标:"+q+"\n"+"鞍点的列下标:"+k); System.out.println("鞍点的值:"+a[q][k]); } } } static int findMax(int a[][],int m){ int max=a[m][0]; int maxCIndex = 0; for(int i=0;i<a[m].length;i++){ if(a[m][i]>max) { max=a[m][i]; maxCIndex=i; } } return maxCIndex; } static int findMin(int a[][],int n){ int min=a[0][n]; int minRIndex = 0; for(int j=0;j<a.length;j++){ if(a[j][n]<min) { min=a[j][n]; minRIndex=j; } } return minRIndex; } }
算法二:
/*找出一个二维数组的鞍点。 鞍点指某数组元素的值在该行上最大、在列上最小。也可能没有鞍点*/ public class andain2 { public static void main(String[] args) { int a[][]={{13,9,16},{5,4,3},{6,1,11}}; isAndian(a); } static void isAndian(int a[][]){ int row=a.length;//row for(int i=0;i<row;i++){ int k=findMax(a,i); if(isMin(a,i,k)){ System.out.println("鞍点的行下标:"+i+"\n"+"鞍点的列下标:"+k); System.out.println("鞍点的值:"+a[i][k]); } } } static int findMax(int a[][],int m){ int max=a[m][0]; int maxCIndex = 0; for(int i=1;i<a[m].length;i++){ if(a[m][i]>max) { max=a[m][i]; maxCIndex=i; } } return maxCIndex;//m,k(index) } static boolean isMin(int a[][],int m,int n){ int min=a[m][n]; int q=m; for(int j=0;j<a.length;j++){ if(j==m) continue; if(a[j][n]<min) { min=a[j][n]; q=j; } } if(q==m) return true; else return false; } }
3. 一行数字(6个数)向左平移,打印出依次平移的六行
public class movementL { public static void main(String[] args) { int a[]={7,4,8,9,1,5}; sort(a); } static void sort(int a[]){ for(int i=1;i<a.length+1;i++){ for(int j=i-1;j<a.length;j++){ System.out.print(a[j]+" "); } for(int k=0;k<i-1;k++){ System.out.print(a[k]+" "); } System.out.print("\n"); } } }
一行数字(6个数)向右平移,打印出依次平移的六行
public class movementR { public static void main(String[] args) { int a[]={7,4,8,9,1,5}; sort(a); } static void sort(int a[]){ for(int i=1;i<a.length+1;i++){ for(int k=a.length+1-i;k<a.length;k++){ System.out.print(a[k]+" "); } for(int j=0;j<a.length-i+1;j++){ System.out.print(a[j]+" "); } System.out.print("\n"); } } }
4.转圈
import java.io.ObjectInputStream.GetField; import java.util.*; public class zhuanQuan { public static void main(String[] args) { int num1 = zhuanQuan .getNum1(); int num2 = zhuanQuan .getNum2(); sa(num1, num2); } public static int getNum1() { int num1 = 0; Scanner reader = new Scanner(System.in); try { System.out.println("请输入行:"); num1 = reader.nextInt(); } catch (InputMismatchException e) { System.out.println("对不起,您输入的不是数字,请重新输入:"); num1 = getNum1(); } return num1; } public static int getNum2() { int num2 = 0; Scanner reader = new Scanner(System.in); try { System.out.println("请输入列:"); num2 = reader.nextInt(); } catch (Exception e) { System.out.println("对不起,您输入的不是数字,请重新输入:"); num2 = getNum2(); } return num2; } static void sa(int m, int n) { int b[][] = new int[m][n]; int i = 0; int j = n - 1; int p = 0; int k = m - 1; int num = 1; while (num <= m * n) { for (int col = p; col <= j; col++) { b[p][col] = num++; } i++;// 1 if (num > m * n) break; for (int row = i; row <= k; row++) { b[row][j] = num++; } j--;// 3 if (num > m * n) break; for (int col = j; col >= p; col--) { b[k][col] = num++; } k--;// 3 if (num > m * n) break; for (int row = k; row >= i; row--) { b[row][p] = num++; } p++;// 1 } for (int q = 0; q < m; q++) { for (int x = 0; x < n; x++) if (b[q][x] < 10) System.out.print("0" + b[q][x] + " "); else System.out.print(b[q][x] + " "); System.out.println(‘\n‘); } } }
时间: 2024-10-12 16:30:50