楊暉三角塔
1
1 1
1 2 1
1 3 3 1
1 5 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
---
package chap1;
public class t0108 {
public static void main(String args[])throws Exception{
int n[]=new int [10],
i=0, j=0, k=0 ;
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){ // i 需從 1 開始
n[i]=n[i-1]*i;
System.out.println(n[i]);
}
}
}
---
package chap1;
public class t0108 {
public static void main(String args[])throws Exception{
int n[]=new int [10],
i=0, j=0, k=0 ;
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
int value = n[3]/(n[2]*n[1]); //3取1
System.out.println(value);
value = n[3]/(n[1]*n[2]);//3取2
System.out.println(value);
}
}
---
package chap1;
public class t0108 {
public static void main(String args[])throws Exception{
int n[]=new int [10],
i=0, j=0, k=0 ;
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
for( i=0 ; i <=3 ; i++ ){
int value = n[3]/(n[3-i]*n[i]);
System.out.print(value+"\t");
}
}
}
--- 1~9 輸出
package chap1;
public class t0108 {
public static void main(String args[])throws Exception{
int n[]=new int [10],
i=0, j=0, k=0,l=0;
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
for( l=0;l< n.length;l++){
for( i=0 ; i <=l ; i++ ){
int value = n[l]/(n[l-i]*n[i]);
System.out.print(value+"\t");
}
System.out.println();
}
}
}
---
public class t0108 {
public static void main(String args[])throws Exception{
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int n[]=new int [10],
i=0, j=0, k=0,l=0;
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
int sn=3,fn=5;
for( l=sn ; l<= fn ;l++){
for( i=0 ; i <=l ; i++ ){
int value = n[l]/(n[l-i]*n[i]);
System.out.print(value+"\t");
}
System.out.println();
}
}
}
-------------------test
資料檔:
2
7
---
方法一:(存進陣列裡)
package chap1;
import java.io.*;
public class test1021 {
public static void main(String args[])throws Exception{
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int i=0,j=0,l=0;
int sn[]=new int [10],
fn[]=new int [10];
sn[j] = Integer.parseInt(fr.readLine());
System.out.println(sn[j]);
fn[j] = Integer.parseInt(fr.readLine());
System.out.println(fn[j]);
int n[]=new int [10];
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
for( l=sn[j] ; l<=fn[j] ;l++){
for( i=0 ; i <=l ; i++ ){
int value = n[l]/(n[l-i]*n[i]);
System.out.print(value+"\t");
}
System.out.println();
}
}
}
方法二:(直接讀取)
package chap1;
import java.io.*;
public class t0108 {
public static void main(String args[])throws Exception{
BufferedReader fr = new BufferedReader(new FileReader("data.txt"));
int i=0,j=0,l=0;
int sn = Integer.parseInt(fr.readLine()),
fn = Integer.parseInt(fr.readLine());
int n[]=new int [10];
n[0] = 1 ; // = 0!
for( i=1 ; i < n.length ; i++ ){
n[i]=n[i-1]*i;
}
for( l=sn ; l<=fn ;l++){
for( i=0 ; i <=l ; i++ ){
int value = n[l]/(n[l-i]*n[i]);
System.out.print(value+"\t");
}
System.out.println();
}
}
}
-----------------------------
留言列表