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.*; 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 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(); } } }
#include<stdio.h> int main() { int num = 0, arr[30][30] = {0}; scanf("%d", &num); for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { if (i >= j) { if (j == 0 || i == j) arr[i][j] = 1; else arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } if (arr[i][j] != 0) printf("%5d", arr[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> int main(){ int n, i, j, arr[30][30]; scanf("%d", &n); for(i = 0; i < n; i++){ for(int j = 0; j < i + 1; j++) arr[i][j] = (j == 0 || j == i)?1 : arr[i-1][j]+arr[i-1][j-1]; } for(i = 0; i < n; i++){ for(j = 0; j < i + 1; j++) printf("%5d", arr[i][j]); printf("\n"); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int arr[n][n]; for(int i = 0;i<n;i++){ for(int j = 0;j<n;j++){ if(i == j || j == 0) arr[i][j] = 1; else{ arr[i][j] = arr[i-1][j -1 ] + arr[i - 1][j]; } if( i>=j) cout<<setw(5)<<arr[i][j]; } cout<<endl; } }
#include <stdio.h> int main() { int a[35][35],b[15][15]; int i,j,n,m,flag=1,k; scanf("%d",&n); for(i=0;i<n;i++){ a[i][0]=1; a[i][i]=1; } for(i=1;i<n;i++){ for(j=1;j<=i;j++){ a[i][j]=a[i-1][j-1]+a[i-1][j]; } } for(i=0;i<n;i++){ for(j=0;j<=i;j++){ printf("%5d",a[i][j]); } printf("\n"); } }
#include <stdio.h> long int Factorial(int n, int r) { if(n == 0 || n == r) { return 1; } else { int k = (n - r); long int value1 = 1; while(n) { value1 *= (n--); } long int value2 = 1; while(r) { value2 *= (r--); } long int value3 = 1; while(k) { value3 *= (k--); } long int value = 0; value = value1 / (value2 * value3); return value; } } int main() { int n = 0; scanf("%d", &n); int r = 1; printf("%5d\n",r); while(r < n) { long int num = 0; int i = r; for(int j = 0; j <= i; j++) { num = Factorial(i, j); printf("%5ld", num); } r++; printf("\n"); } }
#include <stdio.h> int Yangh(int n, int m) { if(n == m || m == 0) { return 1; } return Yangh(n - 1, m) + Yangh(n - 1, m - 1); } int main() { int n = 0; int i = 0; int j = 0; scanf("%d", &n); for(i = 0; i < n; i++) { for(j = 0; j <= i; j++) { printf("%5d", Yangh(i, j)); } printf("\n"); } return 0; }
#include <stdio.h> int main() { int arr[30][30] = {0}; int i = 0; int j = 0; int n = 0; scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { if (i == j) arr[i][j] = 1; if (j == 0) arr[i][j] = 1; if (i > 1 && j > 0) arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; } } for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { printf("%5d", arr[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> int main() { int n; scanf("%d",&n); int array[100][100]={0}; int i=0,j=0; array[i][j]=1; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j||j==0) { array[i][j]=1; } else{ array[i][j]=(array[i-1][j]+array[i-1][j-1]); } } } for(i=0;i<n;i++) { for(j=0;j<=i;j++) { printf("%5d",array[i][j]); } printf("\n"); } return 0; }
n = int(input().strip()) arr = [1,1] if n==1: print(f"{1:>5}") elif n==2: print(f"{1:>5}{1:>5}") else: print(f"{1:>5}") print(f"{1:>5}{1:>5}") for i in range(n-2): newarr = [1] for j in range(1,len(arr)): temp = arr[j] + arr[j-1] newarr.append(temp) newarr.append(1) arr = newarr for i in newarr: print(f"{i:>5}",end="") print()
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(); } } }
int main() { int n = 0; scanf("%d", &n); int i = 0; int j = 0; int arr[15][15] = {0}; //每行第一列以及最后一列为1 for (i = 0; i < n; i++) { arr[i][0] = 1; arr[i][i] = 1; } for (i = 2; i < n; i++) { for (j = 1; j <= i - 1; j++) { arr[i][j] = arr[i - 1][j] + arr[i-1][j - 1];//每次上方两位不断缩小相加 } } for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }