数组有关的题目

it2023-01-05  56

杨辉三角(输出一个10行的杨辉三角) int[][] yanghui = new int[10][]; for(int i=0;i<yanghui.length;i++) { yanghui[i] = new int[i+1]; yanghui[i][0]=yanghui[i][i]=1; if (i>1) { for (int j = 1; j < yanghui[i].length-1; j++) { yanghui[i][j] = yanghui[i-1][j-1]+yanghui[i-1][j]; } } } for (int i = 0; i < yanghui.length; i++) { for (int j = 0; j < yanghui[i].length; j++) { System.out.print(yanghui[i][j]+" "); } System.out.println(); } 创建长度为6的int数组,要求元素值在1-30之间。随机赋值,且元素值不能重复 int[] a = new int[6]; for (int i = 0; i < a.length; i++) { a[i] = (int)(Math.random()*30)+1; for (int j = 0; j < i; j++) { if (a[i]==a[j]) { i--; break; } } }
最新回复(0)