KiKi知道什么叫杨辉三角之后对杨辉三角产生了浓厚的兴趣,他想知道杨辉三角的前n行,请编程帮他解答。杨辉三角,本质上是二项式(a+b)的n次方展开后各项的系数排成的三角形。其性质包括:每行的端点数为1, 一个数也为1;每个数等于它左上方和上方的两数之和。
KiKi知道什么叫杨辉三角之后对杨辉三角产生了浓厚的兴趣,他想知道杨辉三角的前n行,请编程帮他解答。杨辉三角,本质上是二项式(a+b)的n次方展开后各项的系数排成的三角形。其性质包括:每行的端点数为1, 一个数也为1;每个数等于它左上方和上方的两数之和。
第一行包含一个整数数n。 (1≤n≤30)
包含n行,为杨辉三角的前n行,每个数输出域宽为5。
6
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { arr[i][0] = 1; arr[i][i] = 1; //两边都为1 if (j < i && j > 0) { //中间的性质 arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } System.out.printf("%5d",arr[i][j]); } System.out.println(); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] arr = new int[n][n]; for(int i = 0; i < n; i++) { for(int j = 0; j <= i; j++) { //每行的端点数和最后一个元素为1 if(j == 0 || i == j) { arr[i][j] = 1; System.out.print(String.format("%5d",1)); } else { //每个数等于它左上方和上方两数之和。 arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; System.out.print(String.format("%5d",arr[i][j])); } } System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); //输入的数组 int[][] Martix = new int[n][]; for(int i = 0;i < Martix.length;i++){ //开辟每行数组的空间 Martix[i] = new int[i + 1]; for(int j = 0;j < Martix[i].length;j++){ //每行第一位和最后一位为1 if(j == 0 || j == Martix[i].length - 1){ Martix[i][j] = 1; }else{ //Martix[i - 1][j]:上方的值 //Martix[i - 1][j - 1]:左上方的值 Martix[i][j] = Martix[i - 1][j] + Martix[i - 1][j - 1]; } } } //输出 for(int a = 0;a < Martix.length;a++){ for(int b = 0;b < Martix[a].length;b++){ System.out.printf("%5d",Martix[a][b]); } System.out.println(""); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (i==j || j==0){ arr[i][j]=1; System.out.printf("%5d",1); }else { arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1]; System.out.printf("%5d",arr[i][j]); } } System.out.println(""); } } }
//注意数据的取值范围 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num = scan.nextInt(); for (int i = 0; i < num; i++) { for (int j = 0; j <= i; j++) { System.out.printf("%5d", func1(i, j)); } System.out.println(); } } public static long func1(long i, long j) { return func2(i,j) / func2(j,j); } public static long func2(long i, long j) { long ans = 1; while (j != 0) { ans *= i; j--; i--; } return ans; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] num = new int[n+1]; int[] res = new int[n+1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { if (j == 1 || j == i) { res[j] = 1; System.out.printf("%5d", 1); }else { res[j] = num[j-1] + num[j]; System.out.printf("%5d", res[j]); } } for (int j = 1; j <= i; j++) { num[j] = res[j]; } System.out.println(); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int [][]coe=new int[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j==0) coe[i][j]=1; if(j>i) coe[i][j]=0; if(i>=1 && j>0){ coe[i][j]=coe[i-1][j-1]+coe[i-1][j]; } } } for(int[] x:coe){ for(int k : x){ if(k!=0){ System.out.printf("%5d",k); } } System.out.println(""); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int[][] arr = new int[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j <= i; j++){ if(j == 0 || j == i){ arr[i][j] = 1; }else{ arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; } } } for(int i = 0; i < n; i++){ for(int j = 0; j <= i ; j++){ System.out.printf("%5d",arr[i][j]); } System.out.println(); } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (j == i || j == 0) { arr[i][j] = 1; System.out.printf("%5d", arr[i][j]); } else { arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; System.out.printf("%5d", arr[i][j]); } } System.out.println(); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<List<Integer>> outerList = new ArrayList<>(); for(int i=0;i<n;i++) { List<Integer> inner = new ArrayList<>(); for(int j=0;j<=i;j++) { inner.add(1); } outerList.add(inner); } List<Integer> InnerList = new ArrayList<>(); for(int i=2;i<n;i++) { InnerList = outerList.get(i); for(int j=1;j<InnerList.size()-1;j++) { int num1 = outerList.get(i-1).get(j-1); int num2 = outerList.get(i-1).get(j); InnerList.set(j,num1+num2); } } for(List<Integer> list : outerList) { for(Integer num : list) { System.out.printf("%5d",num); } System.out.println(); } } }第二种简便方法:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); List<List<Integer>> outerList = new ArrayList<>(); for (int i = 0; i < n; i++) { List<Integer> inner = new ArrayList<>(); for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { inner.add(1); } else { Integer integer1 = outerList.get(i - 1).get(j - 1); Integer integer2 = outerList.get(i - 1).get(j); inner.add(1,integer1+integer2); } } outerList.add(inner); } for(List<Integer> list : outerList) { for(Integer num : list) { System.out.printf("%5d",num); } System.out.println(); } } }
import java.util.Scanner; public class Main{ public static void main(String[]args){ Scanner s=new Scanner(System.in); int n=s.nextInt(); int k[][]=new int[n][n]; int i=0; int j=0; for(i=0;i<n;i++){ for(j=0;j<=i;j++){ if(j==0||j==i){ k[i][j]=1; }else{ k[i][j]=k[i-1][j-1]+k[i-1][j]; } System.out.print(String.format("%5d",k[i][j])); } System.out.println(); } } }