java輸出楊輝三角
① java:楊輝三角,輸入n輸出它的前n行
import java.util.Scanner;
public class HelloWorld {
public static void main(String args[]) {
Scanner input =new Scanner(System.in);
System.out.println("輸入n");
int n=input.nextInt();
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (j < i) {
a[i][j] = 1;
if (j == 0) {
a[i][j] = 1;
} else {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
} else {
a[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int k = 1; k <= n - i; k++)
System.out.printf(" ");
for (int j = 0; j <= i; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.printf("\n");
}
}
}
② Java語言楊輝三角
列印楊輝三角代碼如下:
public class woo {
public static void triangle(int n) {
int[][] array = new int[n][n];//三角形數組
for(int i=0;i<array.length;i++){
for(int j=0;j<=i;j++){
if(j==0||j==i){
array[i][j]=1;
}else{
array[i][j] = array[i-1][j-1]+array[i-1][j];
}
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String args[]) {
triangle(9);
}
}
(2)java輸出楊輝三角擴展閱讀:
楊輝三角起源於中國,在歐洲這個表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年發現這一規律的,比楊輝要遲393年。它把二項式系數圖形化,把組合數內在的一些代數性質直觀地從圖形中體現出來,是一種離散型的數與形的優美結合。
楊輝三角具有以下性質:
1、最外層的數字始終是1;
2、第二層是自然數列;
3、第三層是三角數列;
4、角數列相鄰數字相加可得方數數列。
③ 用java編程列印輸出10行楊輝三角形
// 直接復制
public class SanJiao {
public static void main(String[] args) {
int row = 10; // 三角的行數
int[][] result = new int[row][row];
for (int i = 0; i < row; i++) { // 行
result[i][0] = 1;
System.out.print(result[i][0] + "\t");
for (int j = 1; j <= i; j++) { // 列
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
System.out.print(result[i][j] + "\t");
}
System.out.println();
}
}
}
④ 用JAVA程序列印出楊輝三角
||給你來個main方法(10行結果自的)
public static void main(String args[])
{
int a=10;
int b[][];
b=new int[a][];
for(int i=1;i<=10;i++)
{
b[i-1]=new int[i];
}
for(int j=0;j<10;j++)
{
for(int k=0;k<=j;k++)
{
if(j==0||k==0||k==j)//分支
{
b[j][k]=1;//等於1的
continue;
}else
{
b[j][k]=b[j-1][k-1]+b[j-1][k];//計算值
}
}
}
for(int m=0;m<10;m++)
{
for(int n=0;n<=m;n++)
{
System.out.print(b[m][n]+" ");//循環輸出
}
System.out.println(" ");
}
}
⑤ java輸出楊輝三角怎麼修改
就改了2個小地方
for (int k = 0; k < n - i - 1; k++) {
System.out.print(" ");
}
packageshiyan;
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
publicclass實驗4楊輝三角{
publicstaticvoidmain(String[]args)throwsException{
intn=0;
System.out.println("楊輝三角");
for(;;){
retry:for(;;){
System.out.print("請輸入行數n(輸入exit退出):");
InputStreamReaderisr=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(isr);
Strings=br.readLine();
s=s.trim();
booleanb=s.equalsIgnoreCase("exit");
if(b){
return;
}
try{
n=Integer.parseInt(s);
}catch(Exceptione){
System.out.println("輸入非法,請重新輸入!");
continueretry;
}
if(n>0){
break;
}else{
System.out.println("輸入非法,請重新輸入!");
}
}
inta[][]=newint[n][n];
for(inti=0;i<n;i++){
for(intk=0;k<n-i-1;k++){
System.out.print("");
}
for(intj=0;j<i+1;j++){
if(j==0||j==i){
a[i][j]=1;
System.out.print("1");//
System.out.print("");
}else{
a[i][j]=a[i-1][j-1]+a[i-1][j];//
System.out.print(a[i][j]);
Stringstr=String.valueOf(a[i][j]);
str=str.trim();
intl=str.length();
if(l==1){
System.out.print("");
}
if(l==2){
System.out.print("");
}
if(l==3){
System.out.print("");
}
if(l==4){
System.out.print("");
}
}
}
System.out.println();
}
}
}
}
⑥ 用JAVA列印輸出10行楊輝三角形
public class YangHui {
public static void main(String args[]) {
final int ROW = 10;
int a[][] = new int[ROW + 1][];
for (int i = 0; i <= ROW; i++) {
a[i] = new int[i + 1]; // 指定每行的列數
}
yanghui(a, ROW);
}
static void yanghui(int a[][], int ROW) {
for (int i = 0; i <= ROW; i++)
for (int j = 0; j <= a[i].length - 1; j++) {
if (i == 0 || j == 0 || j == a[i].length - 1)
a[i][j] = 1;
else
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
for (int i = 0; i <= ROW; i++) {
for (int j = 0; j <= a[i].length - 1; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
⑦ java 輸出楊輝三角
import java.util.ArrayList;
import java.util.List;
public class Admin {
public static void main(String... args) {
List result = test(100);
show(result);
}
private static List test(int row) {
List result = new ArrayList();
for (int i = 0; i < row; i++) {
Node[] arr = getLastArr(result, i - 1);
Node[] nodes = new Node[i + 1];
for (int j = 0; j < nodes.length; j++) {
Node left = getNode(arr, j - 1);
Node right = getNode(arr, j);
nodes[j] = new Node(1, left, right);
}
result.add(nodes);
}
return result;
}
private static Node getNode(Node[] arr, int idx) {
if (arr == null || idx > arr.length - 1 || idx < 0) {
return null;
}
return (Node) arr[idx];
}
private static Node[] getLastArr(List data, int idx) {
if (data == null || idx > data.size() - 1 || idx < 0) {
return null;
}
return (Node[]) data.get(idx);
}
private static void show(List result) {
for (int i = 0; i < result.size(); i++) {
Node[] nodes = (Node[]) result.get(i);
for (int j = 0; j < nodes.length; j++) {
System.out.print(nodes[j] + " ");
}
System.out.println();
}
}
}
class Node {
private long num = 0;
private Node left;
private Node right;
public Node(int num, Node left, Node right) {
this.num = num;
this.left = left;
this.right = right;
}
public String toString() {
long leftNum = 0;
long rightNum = 0;
if (left != null) {
leftNum = left.num;
}
if (right != null) {
rightNum = right.num;
}
if (leftNum + rightNum == 0) {
return num + "";
}
num = leftNum + rightNum;
return num + "";
}
}
⑧ 輸出楊輝三角形,Java。
publicstaticvoidmain(String[]args)
{
System.out.print("請輸入整數n:");
Scannerin=newScanner(System.in);
intn=in.nextInt();
int[][]iaYong=newint[n][];//先分配n行
System.out.println("楊輝三角形:");
iaYong[0]=newint[1];//分配第一行的列
iaYong[0][0]=1;
for(inti=1;i<iaYong.length;i++)
{
iaYong[i]=newint[i+1];//分配其餘行的列
iaYong[i][0]=1;
for(intj=1;j<i;j++)
iaYong[i][j]=iaYong[i-1][j-1]+iaYong[i-1][j];
iaYong[i][i]=1;
}
for(intline=0;line<n;line++)//上半部
{
for(inti=0;i<n-line-1;i++)//輸出每行的前導空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//輸出數據
System.out.println();
}
for(intline=n-2;line>=0;line--)//下半部
{
for(inti=0;i<n-line-1;i++)//輸出每行的前導空格
System.out.print("");
for(intcol:iaYong[line])
System.out.printf("%3d",col);//輸出數據
System.out.println();
}
}
⑨ 怎樣用java列印楊輝三角,自己輸入行
/**
* 列印楊輝三角
功能描述:使用多重循環列印6階楊輝三角
* @author pieryon
*
*/
public class YHSJ {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入行號:");
int m = in.nextInt();
int n = 2*m-1;//列元素數;
int arr[][] = new int[m][n];
for (int i = 0; i < m; i++) { //外循環控制行
for (int j = 0; j < n; j++) { //內循環控制列
if (j<(m-i-1)||(j>=(m+i))) { //輸出等腰三角形兩邊空格
System.out.print(" ");
}else if (j==(m-i-1)||j==(m+i-1)) { //計算輸出等腰三角形兩邊的空格
arr[i][j] = 1;
System.out.print(arr[i][j]);
}else if ((i+j)%2==0&&m%2==0||(i+j)%2==1&&m%2==1) {
System.out.print(" ");
}else {
arr[i][j] = arr[i-1][j-1]+arr[i-1][j+1];
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
}
以上就可以輕松實現楊輝三角的列印了!