Java 帕斯卡的三角形格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19918994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pascal's Triangle Format
提问by Sidarth Shahri
The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed.
任务是在不使用数组的情况下创建帕斯卡三角形。我有产生下面三角形值的方法。该方法接受用户想要打印的最大行数的整数。
public static void triangle(int maxRows) {
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
}
I need to format the values of the triangle such that it looks like a triangle:
我需要格式化三角形的值,使其看起来像一个三角形:
1
1 1
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
I can't for the life of me figure out how to do that. Please answer keeping in mind that I'm a beginner in Java programming.
我一生都无法弄清楚如何做到这一点。请记住我是 Java 编程的初学者。
回答by PM 77-1
In each row you will need to print:
在每一行中,您需要打印:
n
spacesm
numbersn
spaces
n
空间m
数字n
空间
Your job is to figure out n
(which will be zero in the last line) and m
based on row number
.
您的工作是找出n
(在最后一行中为零)并m
基于row number
.
[This is more like a comment but I needed more formatting options than comments provide]
[这更像是评论,但我需要比评论提供的更多格式选项]
回答by Uncle Iroh
This is a good start, where it's homework, I'll leave the rest to you:
这是一个好的开始,这是家庭作业,剩下的交给你:
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--) {
System.out.print(" ");
}
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
回答by Java Devil
You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function
您需要打印空格(就像其他人提到的那样),而且由于这是作业,我将它留给您,但您可能想看看这个方便的小功能
System.out.printf();
Here is a handy reference guide
这是一个方便的参考指南
Also note that you will need to take into account that some numbers are more than 1 digit long!
另请注意,您需要考虑到某些数字的长度超过 1 位!
回答by Nelson Thomas
import java.util.*;
class Mine
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=1;i<n;i++)
{
int size=1;
for(int j=1;j<=i;j++)
{
int a[]=new int[size];
int d[]=new int[size];
for(int k=1;k<=size;k++)
{
a[1]=1;
a[size]=1;
for(int p=1;p<=size;p++)
{
d[p]=a[p];
}
if(size>=3)
{
for(int m=2;m<size;m++)
{
a[m]=d[m]+d[m-1];
}
}
}
for(int y=0;y<size;y++)
{
System.out.print(a[y]);
}
System.out.println(" ");
}
++size;
}
}
}
回答by Irf92
public class HelloWorld{
public static void main(String []args){
int s=7;
int k=1;
int r;
for(int i=1;i<=s;i++){
int num=1;
r=i;
int col=0;
for(int j=1;j<=2*s-1;j++){
if(j <= s-i)
System.out.print(" ");
else if(j >= s+i)
System.out.print(" ");
else{
if(k%2 ==0){
System.out.print(" ");
}
else{
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num+" ");
col++;
}
k++;
}
}
System.out.println("");
k=1;
}
}
}
回答by Boa Hancock
public static long pascalTriangle(int r, int k)
{
if(r == 1 || k <= 1 || k >= r) return 1L;
return pascalTriangle(r-1, k-1) + pascalTriangle(r-1, k);
}
This method allows you to find the kth value of rth row.
此方法允许您找到第 r 行的第 k 个值。
回答by Rida Rukhsar
public static void main(String[] args)
{
int a, num;
for (int i = 0; i <= 4; i++)
{
num = 1;
a = i + 1;
for(int j=4;j>0;j--)
{
if(j>i)
System.out.print(" ");
}
for (int j = 0; j <= i; j++)
{
if (j > 0)
num = num * (a - j) / j;
System.out.print(num + " ");
}
System.out.println();
}
}
Code perfectly prints pascal triangle
代码完美打印帕斯卡三角形
回答by Sanjana Sekar
You can try this code in java. It's simple :)
您可以在java中尝试此代码。这很简单 :)
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();
}
}
}