键盘录入行数,输出打印杨辉三角形 两种格式杨辉三角形
import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入需要打印杨辉三角形的行数:"); int a=sc.nextInt(); print(a);//打印上图第二种格式 println(a);//打印上图第一种格式 } public static void print(int a) { long[][]arr = new long[a][]; for(int i=0;i<arr.length;i++) { arr[i]=new long[i+1]; } for(int i=0;i<arr.length;i++) { for(int j=0;j<arr[i].length;j++) { if(j==0|| j==arr[i].length-1) { arr[i][j]=1; }else { arr[i][j]= arr[i-1][j-1]+arr[i-1][j]; } System.out.print(arr[i][j]+" "); } System.out.println(""); } } public static void println(int a) { long[][]arr = new long[a][]; for(int i=0;i<arr.length;i++) { arr[i]=new long[i+1]; } for(int i=0;i<arr.length;i++) { for(int x=a-i-1;x>0;x--) { System.out.print(" "); } for(int j=0;j<arr[i].length;j++) { if(j==0|| j==arr[i].length-1) { arr[i][j]=1; }else { arr[i][j]= arr[i-1][j-1]+arr[i-1][j]; } System.out.print(arr[i][j]+" "); } System.out.println(""); } } }
将两个一维数组合并,并安照升序排列
public class Test { public static void main(String[] args) { int[] arr= {1,7,9,11,13,15,17,19}; int[] arr1= {2,4,6,8,10}; int[] newArr=harmony(arr, arr1); System.out.println(Arrays.toString(newArr)); } public static int[] harmony(int[] arr, int[] arr1) { int[] newArr= new int[arr.length+arr1.length]; for(int i=0;i<arr.length;i++) { newArr[i]=arr[i]; } for(int i=0;i<arr1.length;i++) { newArr[i+arr.length]=arr1[i]; } System.out.println(Arrays.toString(newArr)); for(int i=0;i<newArr.length;i++) { for(int a=i+1;a<newArr.length;a++) { int temp=0; if(newArr[i]>newArr[a]) { temp=newArr[i]; newArr[i]=newArr[a]; newArr[a]=temp; } } } return newArr; } }
标签:arr,19,newArr,08,System,int,static,2023,public From: https://www.cnblogs.com/zhenaifen/p/17642355.html