到目前为止,我有这个,但我不太确定printPTriangle如何使用代码打印三角形。如果有人可以帮我解决这个问题,我将不胜感激。
public static int factorial(int n) {
if (n == 1) {
return 1;
}
return n * (factorial(n - 1));
}
public static int pascalsNumber(int x, int y) {
return factorial(x)/(factorial(y) * factorial((x - y))); //Using combinations formula
}
public static void printPTriangle(int z) {
}
答案 0 :(得分:1)
我也是Java的初学者,正在开发Pascals三角形。我喜欢上面指出的格式并将其引入我的代码中。我把间距做得更大,以便考虑更大的三角形。
import java.util.Scanner;
public class Pascal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the row number up to which Pascal's triangle has to be printed: ");
int row = scanner.nextInt();
print(row);
scanner.close();
}
public static void print(int n) {
for (int i = 0; i < n; i++) {
System.out.format("%"+(n-i)*3+"s","");
for (int j = 0; j <= i; j++) {
System.out.format("%6d",(pascal(i, j)));
}
System.out.println();
}
}
public static int pascal(int i, int j) {
if (j == 0) {
return 1;
} else if (j == i) {
return 1;
} else {
return pascal(i - 1, j - 1) + pascal(i - 1, j);
}
}
}
答案 1 :(得分:0)
试试这个,
public class PascalTriangle {
public static void main(String[] args) {
int rows = 10;
for(int i =0;i<rows;i++) {
int number = 1;
System.out.format("%"+(rows-i)*2+"s","");
for(int j=0;j<=i;j++) {
System.out.format("%4d",number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
请注意上面使用的格式化命令,以创建格式正确的三角形。 %4d指示格式化程序在4个空格内打印数字。
答案 2 :(得分:0)
以下是解决方案:
public static void main(String[] args)
{
pascal(1,10);
}
static int pascal(int start, int end)
{
if(start>=end)
return 0;
int number = 1;
System.out.format("%"+(end-start)*2+"s","");
pascal2(start,number,0);
System.out.println();
return pascal(start+1,end);
}
static int pascal2(int start,int number,int end)
{
if(end>start)
return 1;
System.out.format("%4d",number);
return pascal2(start,number * (start - end) / (end + 1),end+1);
}
答案 3 :(得分:0)
我只是对Java类的编程挑战感到困惑,无法在仅使用递归且无循环的任何地方找到帮助。因此,经过数小时的痛苦之后,一切都结束了。它所需要的只是一个主方法或演示类,以创建此PascalsTriangle
类的实例并使用行数对其进行初始化。
public class PascalsTriangle {
private StringBuilder str; // StringBuilder to display triangle
/**
* Starts the process of printing the Pascals Triangle
* @param rows Number of rows to print
*/
public PascalsTriangle(int rows) {
str = new StringBuilder();
printTriangle(rows, str);
}
/**
* Uses recursion to function as an "outer loop" and calls
* itself once for each row in triangle. Then displays the result
* @param row The number of the row to generate
* @param str StringBuilder to insert each row into
*/
public static void printTriangle(int row, StringBuilder str) {
// calls itself until row equals -1
if (row >= 0) {
// calls lower function to generate row and inserts the result into front of StringBuilder
str.insert(0, getRow(row, 0) + "\n");
// calls itself with a decremented row number
printTriangle(row - 1, str);
} else {
// when the base case is reached - display the result
JOptionPane.showMessageDialog(null, str);
System.exit(0);
}
}
/**
* Uses recursion to act as the "inner loop" and calculate each number in the given row
* @param rowNumber Number of the row being generated
* @param elementNumber Number of the element within the row (always starts with 0)
* @return String containing full row of numbers or empty string when base case is reached
*/
public static String getRow(int rowNumber, int elementNumber) {
// calls itself until elementNumber is greater than rowNumber
if (elementNumber <= rowNumber) {
// calculates element using combinations formula: n!/r!(n-r)!
int element = fact(rowNumber) / (fact(elementNumber) * (fact(rowNumber - elementNumber)));
// calls itself for each element in row and returns full String
return element + " " + getRow(rowNumber, elementNumber + 1);
} else return "";
}
/**
* Helper function that uses recursion to calculate factorial of given integer
* @param n Number to calculate factorial
* @return Factorial
*/
public static int fact(int n) {
if (n <= 0)
return 1;
else
return n * fact(n - 1);
}