當前位置:首頁 » 編程語言 » java輸出楊輝三角

java輸出楊輝三角

發布時間: 2023-02-02 02:00:00

A. java怎麼用一個一維數組輸出楊輝三角(補充完整下列代碼)

public class ArrayExample{ public static void main(String[] args){ int i=1; int yh[] = new int[8]; for(i=0;i<8;i++) {

B. java數據結構列印楊輝三角形總是少個1

直接定義一個「lianxi」類,定義一個二維數組a[][];

C. JAVASCRIPT輸出楊輝三角

代碼如下:

function print(v){

if (typeof v == "number") {

var w = 30;

if(n>30) w = (n-30) + 40;

var s = '<span style="padding:4px 2px;display:inline-block;text-align:center;width:' + w + 'px;">'+v+'</span>';

document.write(s);

}else{

document.write(v);

}

}

var n = prompt("請輸入冪數:",9);

n = n - 0;

var t1 = new Date();

var a1 = [1,1];

var a2 = [1,1];

print('<div style=text-align:center;">');

for (var i = 0;i <=n;i++![在這里插入圖片描述](http://c.biancheng.net/uploads/allimg/190830/6-1ZS015492BL.gif)){

for (var j = 1; j < i + 2; j++) {

print(c(i,j));

}

print("<br />");

}

print("</div>");

var t2 = new Date();

print("<p style='text-align:center;'>耗時為(毫秒):"+(t2-t1)+"</p>");

function c(x,y){

if ((y == 1) || (y == x + 1)) return 1;

return c(x-1,y-1) + c(x-1,y);

}

(3)java輸出楊輝三角擴展閱讀

楊輝三角的特點:

1、每個數等於它上方兩數之和。

2、每行數字左右對稱,由1開始逐漸變大。

3、第n行的數字有n項。

4、前n行共[(1+n)n]/2 個數。

5、第n行的m個數可表示為C(n-1,m-1),即為從n-1個不同元素中取m-1個元素的組合數。

6、第n行的第m個數和第n-m+1個數相等 ,為組合數性質之一。

7、每個數字等於上一行的左右兩個數字之和。可用此性質寫出整個楊輝三角。即第n+1行的第i個數等於第n行的第i-1個數和第i個數之和,這也是組合數的性質之一。

D. 用JAVA編寫楊輝三角

我的絕對正確 書上抄的 還驗證過!
public static void main(String[]args){
int r=6;
int a[][]=new int[r+1][]; //表示6個一維數組組成
for(int i=0;i<=r;i++){
a[i]=new int[i+1];} //表示使用for循環為一維數組指定列數
YangHui(a,r);
}
static void YangHui(int a[][],int r){
for(int i=0;i<=r;i++){
for(int j=0;j<a[i].length;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<=r;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");}
System.out.println();}
}
}

E. 楊輝三角形(像等邊三角形一樣)輸出

public static void printStr77() {

int i, j;
int h = 7;
int yanghui[][] = new int[7][];
System.out.println("楊輝三角形");
for (i = 0; i < yanghui.length; i++) {
yanghui[i] = new int[i + 1];
yanghui[0][0] = 1;
}
for (i = 1; i < yanghui.length; i++) {
yanghui[i][0] = 1;
for (j = 1; j < yanghui[i].length - 1; j++) {
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j];
}
yanghui[i][yanghui[i].length - 1] = 1;
}
for (i = 0; i < yanghui.length; i++) {

printBlack(yanghui.length - i);

for (j = 0; j < yanghui[i].length; j++)
System.out.print(yanghui[i][j] + " ");
System.out.println();
}
}

public static void printBlack(int count){
for (int i = 0 ; i < count ; i ++) {
System.out.print(" ");
}
}

但是如果數字超過兩位數三位數時,還是會出問題的。
這個要先指定行數然後確定每個字的輸出規則,挺麻煩的。

F. java編寫 使用二維數組存儲楊輝三角並列印輸出。

使用二維數組存儲楊輝三角並列印輸出的Java程序如下

publicclassYangHui{
publicstaticvoidmain(String[]args){
finalintROW=5;//指定楊輝三角形的行數
inta[][]=newint[ROW+1][];
for(inti=0;i<=ROW;i++){
a[i]=newint[i+1];//指定每行的列數
}
for(inti=0;i<=ROW;i++)
for(intj=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(inti=0;i<=ROW;i++){
for(intj=0;j<=ROW-i;j++)
System.out.print(" ");
for(intj=0;j<=a[i].length-1;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}

運行結果

G. 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);

}

}

(7)java輸出楊輝三角擴展閱讀

楊輝三角起源於中國,在歐洲這個表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年發現這一規律的,比楊輝要遲393年。它把二項式系數圖形化,把組合數內在的一些代數性質直觀地從圖形中體現出來,是一種離散型的數與形的優美結合。

楊輝三角具有以下性質:

1、最外層的數字始終是1;

2、第二層是自然數列;

3、第三層是三角數列;

4、角數列相鄰數字相加可得方數數列。

H. 編寫一個JavaApplication,列印8行楊輝三角形(使用數組,不用數組各做一次)

c語言輸出楊輝三角
直角三角形楊輝三角
//c語言,求直角的
#include<stdio.h>
#define M 10
void main()
{
int a[M][M], i , j ;
for(i=0;i<M;i++)
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%d",a[i][j]);
if(i==j)printf("\n");
}
}

使用數組列印金字塔型楊輝三角
#include<stdio.h>
void main()
{
int a[10][10],i,j;
for(i=0;i<10;i++)
{
for(j=10;j>=i;j--)
printf("%2c",' ');/*兩個空格*/
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
printf("%3d ",a[i][j]); /*%3d後一個空格*/
if(i==j)
printf("\n");
}
}
}
不用數組輸出金字塔形楊輝三角
#include<stdio.h>
#define N 10
void main()
{
unsigned int i,j,k;
unsigned int b,c;
for(i=0;i<N;i++)
{
for(j=N;j>i;j--)
printf("");
for(j=0;j<=i;j++)
{
b=c=1;
if(j>=1)
{
for(k=i-j+1;k<=i;k++)
b*=k;
for(k=1;k<=j;k++)
c*=k;
}
printf("%4d",b/c);
}
printf("\n");
}
}
註解:
在列印楊輝三角時通常用到楊輝三角的兩個性質。
第一個就是楊輝三角中除了最外層(不包括楊輝三角底邊)的數為1外,其餘的數都是它肩上兩個數之和。用數組輸出楊輝三角就用這個性質。
第二個性質是楊輝三角的第n行恰好是C(n,0)~C(n,n)。這里的C表示組合。不用數組輸出楊輝三角就用這個性質。把楊輝三角的前15行保存在文本文件中#include<stdio.h>
#include<stdlib.h>
#define M 15
void main()
{
FILE *out;
if((out=fopen("D:\\text_1.txt","w"))==NULL)
{
printf("Error!\n");
exit(0);
}
int a[M][M],i,j;
for(i=0;i<M;i++)
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
fprintf(out,"%5d",a[j]);
if(i==j)
fputc('\n',out);
}
fclose(out);
}
用二維數組輸出前十行:
#include <stdio.h>
int main ()
{
int a[10][10],i,j;
for(i=0;i<10;i++)
{
a[i][i]=1;
a[i][0]=1;
}
for (i=2;i<10;i++)
for (j=1;j<=i-1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=0;i<10;i++)
{
for (j=0;j<=i;j++)
printf("%6d",a[i][j]);
printf("\n");
}
printf("\n");
return 0;
}
編輯本段VB輸出楊輝三角
Private Sub Form_click()
n = Val(Text1.Text)
ReDim a(n + 1, n + 1), b(n + 1, n + 1)
Cls
k = 8
For i = 1 To n
Print String((n - i) * k / 2 + 1, " ");
For j = 1 To i
a(i, 1) = 1
a(i, i) = 1
a(i + 1, j + 1) = a(i, j) + a(i, j + 1)
b(i, j) = Trim(Str(a(i, j)))
Print b(i, j); String(k - Len(b(i, j)), " ");
Next j
Print
Next i
End Sub
創建一個text和command,在text中輸入所需行數,點擊command即可。一個數在楊輝三角出現的次數由1開始,正整數在楊輝三角形出現的次數為∞:1, 2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 2, 4, ... (OEIS:A003016)。最小而又大於1的數在賈憲三角形至少出現n次的數為2, 3, 6, 10, 120, 120, 3003, 3003, ... (OEIS:A062527)
除了1之外,所有正整數都出現有限次。
只有2出現剛好一次。
6,20,70等出現三次。
出現兩次和四次的數很多。
還未能找到出現剛好五次的數。
120,210,1540等出現剛好六次。(OEIS:A098565)
因為丟番圖方程


有無窮個解,所以出現至少六次的數有無窮個多。
其解答,是




其中Fn表示第n個斐波那契數(F1 = F2 = 1)。
3003是第一個出現八次的數。
一道NOIP楊輝三角題目:
#include<stdio.h>
#define maxn 50
const int y=2009;
int main()
{
int n,c[maxn][maxn],i,j,s=0;
scanf("%d",&n);
c[0][0]=1;
for(i=1;i<=n;i++)
{
c[i][0]=1;
for(j=1;j<i;j++)
c[i][j]=c[i-1][j-1]+c[i-1][j];
c[i][i]=1;
}
for(i=0;i<=n;i++)
s=(s+c[n][i])%y;
printf("%d\n",s);
return 0;
此為利用數組求和
Java實現
代碼:
public class YhuiTest {
public static void main(String[] args) {
final int Row = 6;
int yh[][] = new int[Row][Row];
for (int i = 0; i < Row; i++) {
yh[i][0] = 1;
yh[i][i] = 1;
}
for (int i = 2; i < Row; i++) {
for (int j = 1; j < Row; j++) {
yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j];
}
}
for (int i = 0; i < Row; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(yh[i][j] + " ");
}
System.out.println();
}
}
}

代碼
結果:
C++輸出楊輝三角
//單數組動態規劃輸出楊輝三角,以下截止第31行
#include <iostream>
using namespace std;
#define MAXH 31
int main()
{
int i,j;
unsigned long num[MAXH]={0};
num[0] = 1;
for(i = 0; i < MAXH; i++)
{
for(j = i; j > 0; j--)
{
num[j] = num[j] + num[j - 1];//A[i,j]=A[i,j-1]+A[i,j]
cout<<num[j]<<" ";
}
cout<<"1"<<endl;
}
return 0;
}
數組輸出楊輝三角
/*直角三角形*
#include<iostream>
using namespace std;
int main()
{
int h,i,j;
cout<<"請輸入楊輝三角的高度:"<<endl;
cin>>h;
int a[10][10];
for(i=0;i<10;i++)
{
a[i][i]=1;
a[i][0]=1;
}
for(i=2;i<10;i++)
for(j=1;j<=i-1;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
for(i=0;i<=h;i++)
{
for(j=0;j<=i;j++)
cout<<a[i][j]<<'\t';
cout<<endl;
}
return 0;
}
/*等腰三角形*
#include<iostream>
using namespace std;
int main()
{
int i,j,h,a[10][10];
cout<<"請輸入楊輝三角的高度:"<<endl;
cin>>h;
for(i=0;i<=h;i++)
{
for(j=0;j<=i;j++)
{
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
}
}
for(i=0;i<=h;i++)
{
for(j=h;j>=i;j--)
cout<<" ";
for(j=0;j<=i;j++)
{
cout<<a[i][j]<<'\t';
if(i==j)
cout<<endl;
}
}
return 0;
}
遞歸方法輸出直角楊輝三角
#include<iostream>
using namespace std;
int computeTriangleElement(int level,int index);
void yanghuiTriangle(int level);
void yanghuiTriangle(int level)
{
for(int i=1;i<=level;i++)
{
for(int j=1;j<=i;j++)
{
cout<<computeTriangleElement(i,j)<<' ';
}
cout<<endl;
}
}
int computeTriangleElement(int level,int index)
{
if(index==1||index==level)
return 1;
return computeTriangleElement(level-1,index-1)+computeTriangleElement(level-1,index);
}
int main()
{
int level;
cout<<"請輸入楊輝三角的高度:"<<endl;
cin>>level;
yanghuiTriangle(level);
return 0;
}
隊列輸出直角楊輝三角
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define MAX_QUEUE 100
typedef int DataType;
typedef struct
{
DataType elem[MAX_QUEUE];
int front;
int rear;
}LinkQueue;
int InitQueue(LinkQueue *);
void EnQueue(LinkQueue *,DataType);
void DeQueue(LinkQueue *,DataType *);
void GetFront(LinkQueue,DataType *);
int QueueEmpty(LinkQueue);
void YangHuiTriangle(int );
int main()
{
int n=1;
printf("please enter a number: ");
scanf("%d",&n);
if(n<=0)
{
printf("ERROR!\n");
exit(0);
}
YangHuiTriangle(n);
return 0;
}
int InitQueue(LinkQueue *Q)
{
Q->front=Q->rear=-1;
return 1;
}
void EnQueue(LinkQueue *Q,DataType e)
{
if((Q->rear+1)%MAX_QUEUE==Q->front)
exit(OVERFLOW);
else
{
Q->rear=(Q->rear+1)%MAX_QUEUE;
Q->elem[Q->rear]=e;
}
}
void DeQueue(LinkQueue *Q,DataType *e)
{
if(QueueEmpty(*Q))
{
printf("queue is empty\n");
exit(0);
}
else
{
Q->front=(Q->front+1)%MAX_QUEUE;
*e=Q->elem[Q->front];
}
}
void GetFront(LinkQueue Q,DataType *e)
{
if(QueueEmpty(Q))
{
printf("queue is empty\n");
exit(0);
}
else
*e=Q.elem[(Q.front+1)%MAX_QUEUE];
}
int QueueEmpty(LinkQueue Q)
{
if(Q.front==Q.rear)
return 1;
else
return 0;
}
void YangHuiTriangle(int n)
{
LinkQueue Q;
int i,j,k,t,s,e;
InitQueue(&Q);
for(i=0;i<n;i++)
printf(" ");
printf(" 1\n");
EnQueue(&Q,1);
EnQueue(&Q,1);
for(i=1;i<n;i++)
{
for(k=0;k<n-i;k++)
printf(" ");
EnQueue(&Q,1);
for(j=0;j<i;j++)
{
DeQueue(&Q,&t);
printf(" %3d ",t);
GetFront(Q,&s);
e=t+s;
EnQueue(&Q,e);
}
EnQueue(&Q,1);
DeQueue(&Q,&t);
printf(" %d\n",t);
}
}

I. java編寫楊輝三角~~~

  • 楊輝三角線的推理:

楊輝三角形性質:


每行數字左右對稱,由 1 開始逐漸變大,然後變小,回到 1。


第 n 行的數字個數為 n 個。


第 n 行數字和為 2^(n-1) 。


每個數字等於上一行的左右兩個數字之和。可用此性質寫出整個楊輝三角形。


第 n 行的第 1 個數為 1,第二個數為 1× (n-1) ,第三個數為 1× (n-1) × ( n-2) /2,第四個數為1× (n-1) × (n-2) /2× (n-3) /3…依此類推。

使用一個二維數組 yh[][] 存儲楊輝三角形的數據,行和列的大小為所需要輸出的行數 Row(本程序中 Row 為 10)。


使用 for 循環使楊輝三角中除了最外層(不包括楊輝三角底邊)的數為 1 ;


使用語句 yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j] 使第 i 行第 j 列的數據等於第(i-1) 行


第(j-1)列的數據與第(i-1)行第(j)列的數據之和,即每個數字等於上一行的左右兩個數字之和。

  • 代碼的實現

  • packagecom.practice;

    publicclassYangHuiSanJiao
    {
    publicstaticvoidmain(String[]args){
    int[][]a=newint[10][10];
    for(intn=0;n<10;n++)
    {
    a[n][0]=1;
    a[n][n]=1;
    }
    for(intn=2;n<10;n++)
    {
    for(intj=1;j<n;j++)
    {
    a[n][j]=a[n-1][j-1]+a[n-1][j];
    }
    }

    for(intn=0;n<10;n++)
    {
    for(intk=0;k<2*(10-n)-1;k++)
    {
    System.out.print("");
    }
    for(intj=0;j<=n;j++)
    {
    System.out.print(a[n][j]+"");
    }
    System.out.println();
    }

    }
    }
熱點內容
安卓外部資源怎麼下載 發布:2024-03-29 04:01:17 瀏覽:244
華為被加密碼的相冊在哪裡查看 發布:2024-03-29 04:00:27 瀏覽:747
自動欣悅版有哪些配置 發布:2024-03-29 03:48:26 瀏覽:287
如何用腳本搶 發布:2024-03-29 03:01:59 瀏覽:120
火影忍者手游配置怎麼調 發布:2024-03-29 02:53:53 瀏覽:103
編程畫櫻花 發布:2024-03-29 02:11:24 瀏覽:473
騰訊雲伺服器1mb老掉線 發布:2024-03-29 01:56:11 瀏覽:215
執行sql語句的存儲過程 發布:2024-03-29 01:52:37 瀏覽:697
婚紗攝影腳本 發布:2024-03-29 01:47:40 瀏覽:901
我的世界伺服器咋開外掛 發布:2024-03-29 01:07:45 瀏覽:456