1个回答
2014-01-14
展开全部
import java.io.*;
public class ConsoleReader
{
private String temp;
BufferedReader reader;
public ConsoleReader()
{
reader = new BufferedReader(new InputStreamReader(System.in));
}
public int getInt()
{
try
{
temp = reader.readLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return Integer.parseInt(temp);
}
}
//数组实现杨辉三角
import com.mfg.console.ConsoleReader;
public class YanHui
{
int[][] a;
public YanHui()
{
ConsoleReader console=new ConsoleReader();
System.out.print("请输入要求的杨辉三角的级数:");
int n=console.getInt();
a=new int[n][];
for(int i=0;i a[i]=new int[i+1];
//计算杨辉三角
a[0][0]=1;
for(int i=1;i {
a[i][0]=1;
for(int j=1;j {
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
a[i][a[i].length-1]=1;
}
//打印杨辉三角
for(int i=0;i {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
public static void main(String[] args)
{
YanHui yanHui1 = new YanHui();
}
}
public class ConsoleReader
{
private String temp;
BufferedReader reader;
public ConsoleReader()
{
reader = new BufferedReader(new InputStreamReader(System.in));
}
public int getInt()
{
try
{
temp = reader.readLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
return Integer.parseInt(temp);
}
}
//数组实现杨辉三角
import com.mfg.console.ConsoleReader;
public class YanHui
{
int[][] a;
public YanHui()
{
ConsoleReader console=new ConsoleReader();
System.out.print("请输入要求的杨辉三角的级数:");
int n=console.getInt();
a=new int[n][];
for(int i=0;i a[i]=new int[i+1];
//计算杨辉三角
a[0][0]=1;
for(int i=1;i {
a[i][0]=1;
for(int j=1;j {
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
a[i][a[i].length-1]=1;
}
//打印杨辉三角
for(int i=0;i {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
public static void main(String[] args)
{
YanHui yanHui1 = new YanHui();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询