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
请大家帮忙 用java怎样把它打出来.....
5 个解决方案
#1
#2
public class Triangle
{
public static void main(String[] args)
{
Triangle.print();
}
public static void print()
{
int[][] a = new int[10][10];
for(int i=0;i<10;i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for(int i=2;i<10;i++)
{
for(int j=1;j<=i;j++)
{
a[i][j] = a[i-1][j-1]+a[i-1][j];
}
}
for(int i=0;i<10;i++)
{
for(int j =0;j<=i;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
这个是用写字本做的凑合着看吧 呵呵 楼主的题目是杨辉三角,不过楼主貌似写错了~
#3
#4
public class Main {
public static void main(String[] args) {
printMathTriangle(null, true, 0);
System.out.println("========================================");
printMathTriangle(null, true, 1);
System.out.println("========================================");
printMathTriangle(null, true, 2);
System.out.println("========================================");
printMathTriangle(null, true, 3);
System.out.println("========================================");
printMathTriangle(null, true, 6);
}
public static final int[] initValues = new int[] { 1, 2, 1 };
public static void printMathTriangle(int[] values, boolean isFirstTime, final int totalTimes) {
if (totalTimes < 1) {
System.out.println("No result!");
return;
} else if (totalTimes == 1) {
System.out.println(1);
return;
} else if (totalTimes == 2) {
System.out.println("1");
System.out.println("1 2 1");
return;
} else if (totalTimes > 2 && isFirstTime) {
System.out.println("1");
System.out.println("1 2 1");
printMathTriangle(initValues, false, totalTimes);
return;
}
int[] nextInitValues = new int[values.length + 1];
for (int i = 0; i < values.length + 1; i++) {
int result = 0;
if (i == 0) {
result = 0 + values[i];
} else if (i == values.length) {
result = values[i - 1] + 0;
} else {
result = values[i - 1] + values[i];
}
System.out.print(result + " ");
nextInitValues[i] = result;
}
System.out.println();
if (nextInitValues.length - 1 == totalTimes) {
return;
}
printMathTriangle(nextInitValues, false, totalTimes);
}
}
#5
谢谢 我更期待有更好的方法哦
#1
#2
public class Triangle
{
public static void main(String[] args)
{
Triangle.print();
}
public static void print()
{
int[][] a = new int[10][10];
for(int i=0;i<10;i++)
{
a[i][0] = 1;
a[i][i] = 1;
}
for(int i=2;i<10;i++)
{
for(int j=1;j<=i;j++)
{
a[i][j] = a[i-1][j-1]+a[i-1][j];
}
}
for(int i=0;i<10;i++)
{
for(int j =0;j<=i;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
这个是用写字本做的凑合着看吧 呵呵 楼主的题目是杨辉三角,不过楼主貌似写错了~
#3
#4
public class Main {
public static void main(String[] args) {
printMathTriangle(null, true, 0);
System.out.println("========================================");
printMathTriangle(null, true, 1);
System.out.println("========================================");
printMathTriangle(null, true, 2);
System.out.println("========================================");
printMathTriangle(null, true, 3);
System.out.println("========================================");
printMathTriangle(null, true, 6);
}
public static final int[] initValues = new int[] { 1, 2, 1 };
public static void printMathTriangle(int[] values, boolean isFirstTime, final int totalTimes) {
if (totalTimes < 1) {
System.out.println("No result!");
return;
} else if (totalTimes == 1) {
System.out.println(1);
return;
} else if (totalTimes == 2) {
System.out.println("1");
System.out.println("1 2 1");
return;
} else if (totalTimes > 2 && isFirstTime) {
System.out.println("1");
System.out.println("1 2 1");
printMathTriangle(initValues, false, totalTimes);
return;
}
int[] nextInitValues = new int[values.length + 1];
for (int i = 0; i < values.length + 1; i++) {
int result = 0;
if (i == 0) {
result = 0 + values[i];
} else if (i == values.length) {
result = values[i - 1] + 0;
} else {
result = values[i - 1] + values[i];
}
System.out.print(result + " ");
nextInitValues[i] = result;
}
System.out.println();
if (nextInitValues.length - 1 == totalTimes) {
return;
}
printMathTriangle(nextInitValues, false, totalTimes);
}
}
#5
谢谢 我更期待有更好的方法哦