Realization of one-dimensional array and two-dimensional array of Yang Hui's triangle (printed out triangle)

Source: Internet
Author: User

nonsense, direct code, code comments clarify everything ... Two-dimensional array implementation

/** * Yang Hui's triangular two-dimensional array implementation * @author last D * * * Package experiment;

Import Java.util.Scanner;
		public class Triangle2d {public static void main (string[] args) {System.out.println ("Enter the specified number of Yang Hui's Triangle lines:");
		Scanner sc = new Scanner (system.in);
		int n = sc.nextint ();
		Sc.close ();
		int[][] temp = tri (n);
		System.out.println ("Printed Yang Hui's triangle is as follows:");
	Print (temp, n);   } Static int[][] tri (int n) {int[][] yh = new int[n][];          Assign space to one dimension yh[0] = new INT[1];
		Assigning space to two-dimensional yh[0][0] = 1;    for (int i = 1; i < n; i++) {Yh[i] = new int[i+1];
			Give the two-dimensional allocation space yh[i][0] = yh[i][i] = 1;
	for (int j = 1; j < I; J +) {Yh[i][j] = Yh[i-1][j-1] + yh[i-1][j];//value is the value of the previous line diagonally and} return YH;
		static void print (int[][] temp, int n) {int col = (2*n-1)/2;
			for (int i = 0; i < n; i++) {//Print space for (int k = 0; k < col; k++) {System.out.print ("");
			//print numeric for (int j = 0; J <= i; j +) {System.out.print (temp[i][j]+ "");
	} System.out.println ("\ t");		col--;
 }
	}
	
}

one-dimensional array implementation
/**
 * Using one-dimensional array to implement Yang Hui's triangle
 * @author Last D * * *

package experiment;

public class Triangle1d {public
	static void Main (string[] args) {
		int num = 5;//Yang Hui's triangle rows
		int[] temp = tri (num) ;
		Print (temp, num);
	}
	
	Static int[] tri (int num) {
		int sum = num * (num + 1)/2;
		int[] D1 = new Int[sum];
		D1[0] = 1;
		for (int i = 1; i < num; i++) {
			int loca = i * (i + 1)/2;//The starting position of the current line
			D1[loca] = D1[loca + i] = 1;
			
			for (int j = Loca + 1; j < Loca + i + j) {
				d1[j] = d1[j-i-1] + d1[j-i];
			}
		return d1;
	}
	
	static void print (int[] temp, int n) {

		int col = (2*n-1)/2;
		for (int i = 0; i < n; i++) {
			//Print space for
			(int k = 0; k < col; k++) {
				System.out.print ("");
			}
			//print numeric
			int loca = i * (i + 1)/2;//The starting position of the current line for
			(int j = Loca; J <= Loca + i; j +) {
				System.out.pri NT (temp[j]+ "");
			}
			System.out.println ("T");
			col--
		}}
	}
	


more please go to the small stack of elder brother

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.