编写程序,打印如下的杨辉三角形:
- 提问者网友:眉目添风霜
- 2021-05-24 02:37
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
c++
- 五星知识达人网友:像个废品
- 2021-05-24 03:39
public static void main(String[] args) {
textYH yang = new textYH();
yang.printYanghuiTriangle(5);
}
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 0 start :1L;
while(num > start) {
result *= num--;
}
return result;
}
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( );
}
}