打印出杨辉三角形(要求打印出10行如下图) 10
1个回答
展开全部
public class Test {
public static void main(String[] args){
int n = 10;
int[] last = new int[n];
for (int i = 0; i < n; ++i)
last[i] = 1;
int[] current = last.clone();
System.out.print("1\n1 1\n");
for (int i = 3; i <= n; ++i) {
for (int j = 1; j < i - 1; ++j) {
current[j] = last[j] + last[j - 1];
}
print(current, i);
int[] t = current;
current = last;
last = t;
}
}
static void print(int[] array, int n) {
for (int i = 0; i < n; ++i)
System.out.print(array[i] + " ");
System.out.println();
}
}
public static void main(String[] args){
int n = 10;
int[] last = new int[n];
for (int i = 0; i < n; ++i)
last[i] = 1;
int[] current = last.clone();
System.out.print("1\n1 1\n");
for (int i = 3; i <= n; ++i) {
for (int j = 1; j < i - 1; ++j) {
current[j] = last[j] + last[j - 1];
}
print(current, i);
int[] t = current;
current = last;
last = t;
}
}
static void print(int[] array, int n) {
for (int i = 0; i < n; ++i)
System.out.print(array[i] + " ");
System.out.println();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询