import javax.swing.JOptionPane;public class test3 {
public static void main(String[] args) {
String num=JOptionPane.showInputDialog("请输入杨辉三角形的行数:50以内的数字");
try{
int fei= Integer.parseInt(num);
}
catch (NumberFormatException e){
System.out.println("请输入数字,不可为空");
return;
}
int n = Integer.valueOf(num);
String hou ="%16d";
String xiao =" ";
//String ff=" ";
Long c=0l;
if(n>50)
System.out.print("请输入正确的值");
else {
/* if(n<=10){ hou="%6d"; xiao=" ";}
else if(n<=20){ hou="%10d"; xiao=" ";}
else if(n<=30) {hou="%12d"; xiao=" ";}
else if(n<=40) {hou="%14d"; xiao=" ";} */
long[][] a=new long[n][n];
for(int i=0;i<n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(int i=2;i<n;i++) {
for(int j=1;j<i;j++) {
a[i][j]=a[i-1][j-1]+a[i-1][j];
c = (a[i][j/2]);}
}
String d = c.toString();
for (int e =1; e<=d.length(); ++e){
hou="%"+(e+1)+"d";
xiao=" ";
}
for(int i=0;i<n;i++)
{
//打印空格
for(int k=0;k<n-i;++k)
System.out.print(xiao);
//打印数字
for(int j=0;j<=i;j++)
System.out.printf(hou,a[i][j]);
System.out.println(" ");
//换行
System.out.println();
}
}
}
} 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 这是5行时候结果 1 1 1 1 2 1 1 3 3 1 1 4 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 这是10行结果如何让空格也动态变化 望大神教教
public static void main(String[] args) {
String num=JOptionPane.showInputDialog("请输入杨辉三角形的行数:50以内的数字");
try{
int fei= Integer.parseInt(num);
}
catch (NumberFormatException e){
System.out.println("请输入数字,不可为空");
return;
}
int n = Integer.valueOf(num);
String hou ="%16d";
String xiao =" ";
//String ff=" ";
Long c=0l;
if(n>50)
System.out.print("请输入正确的值");
else {
/* if(n<=10){ hou="%6d"; xiao=" ";}
else if(n<=20){ hou="%10d"; xiao=" ";}
else if(n<=30) {hou="%12d"; xiao=" ";}
else if(n<=40) {hou="%14d"; xiao=" ";} */
long[][] a=new long[n][n];
for(int i=0;i<n;i++)
{
a[i][0]=1;
a[i][i]=1;
}
for(int i=2;i<n;i++) {
for(int j=1;j<i;j++) {
a[i][j]=a[i-1][j-1]+a[i-1][j];
c = (a[i][j/2]);}
}
String d = c.toString();
for (int e =1; e<=d.length(); ++e){
hou="%"+(e+1)+"d";
xiao=" ";
}
for(int i=0;i<n;i++)
{
//打印空格
for(int k=0;k<n-i;++k)
System.out.print(xiao);
//打印数字
for(int j=0;j<=i;j++)
System.out.printf(hou,a[i][j]);
System.out.println(" ");
//换行
System.out.println();
}
}
}
} 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 这是5行时候结果 1 1 1 1 2 1 1 3 3 1 1 4 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 这是10行结果如何让空格也动态变化 望大神教教
System.out.printf("%5d%5d%5d", 1, 1, 1);
System.out.printf("%5d", 1);
System.out.printf("%5d%5d%5d", 1, 1, 1);
Yanghui yang = new Yanghui();
yang.printYanghuiTriangle(13);
} /**
* 生成指定行数的杨辉三角形
*
* @param lines 杨辉三角形的行数
*/
public void printYanghuiTriangle(int lines) {
if(lines < 1) {
throw new IllegalArgumentException("lines must be great than 0.");
}
if(lines > 30) {
throw new IllegalArgumentException("lines is too big.");
}
int[] line = new int[lines];
int maxLen = getMaxLen(lines);
for(int i = 0; i < lines; i++) {
line[0] = line[i] = 1;
for(int j = 1, k = i / 2, pre = line[0]; j <= k; j++) {
int cur = line[j];
line[i - j] = line[j] += pre;
pre = cur;
}
printLine(line, i + 1, maxLen);
}
} /**
* 根据指定行数的杨辉三角形,计算其中最大数字的长度
* @param lines 杨辉三角形的行数
* @return 最大数字的长度
*/
private int getMaxLen(int lines) {
int k = lines / 2;
long maxNum = factorial(k + 1, lines - 1) / factorial(1, lines - 1 - k);
return getLength(maxNum);
} /**
* 阶乘计算
* @param start 阶乘计算的起始数字
* @param num 阶乘计算的终止数字
* @return 阶乘计算结果
*/
private long factorial(int start, int num) {
long result = start > 0 ? start : 1L;
while(num > start) {
result *= num--;
}
return result;
} /**
* 根据指定数字计算数字的长度
* @param num 数字
* @return 数字的长度
*/
private int getLength(long num) {
int len = 0;
while(num > 0L) {
num /= 10L;
len++;
}
return len;
} private void printLine(int[] yanghui, int line, int width) {
printSpaces((yanghui.length - line) * width); for(int i = 0; i < line; i++) {
if(i > 0) {
printSpaces(width);
}
printSpaces(width - getLength(yanghui[i]));
System.out.print(yanghui[i]);
}
System.out.println();
if(width > 1) {
System.out.println();
}
} private void printSpaces(int spaceCount) {
for(int i = 0; i < spaceCount; i++) {
System.out.print(" ");
}
}
} 1 1 1 1 2 1 1 3 3 1 1 4 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 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1利用二项式定理,计算出每行最大数值的位数,用以显示成三角形的形状。
为了显示成三角形的形状,这里耗费了大量的代码。
* | 1 2 3 4 5 6 7 8 9
-------|------------------------------------
1 | 1
2 | 2 4
3 | 3 6 9
4 | 4 8 12 16
5 | 5 10 15 20 25
6 | 6 12 18 24 30 36
7 | 7 14 21 28 35 42 49
8 | 8 16 24 32 40 48 56 64
9 | 9 18 27 36 45 54 63 72 81java输出九九乘法表