杨辉三角:
import java.util.*;
public class yanghui {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“输入层数:”);
int cengshu = sc.nextInt();
int x[][]=new int[cengshu][cengshu];
int y=1;
for(int i=0;i<cengshu;i++)
{
for(int j=0;j<y;j++)
{
if(j==0||j==i)
{
x[i][j]=1;
}
else
{
x[i][j]=x[i-1][j-1]+x[i-1][j];
}
System.out.print(x[i][j]+”\t”);
}
y++;
System.out.println();
}
}
}
输出结果:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
…… ……
根据输入的层数决定.
“杨辉三角”3 条评论
怎么不是赵辉三角啊……
哇卡卡卡。。。
wordpress的插件为:http://fairyfish.net/2009/01/16/thread-twitter/
感谢大王,娃卡卡,一起推哇