Pascal's Triangle Format

2019-01-18 05:01发布

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.

8条回答
你好瞎i
2楼-- · 2019-01-18 05:23

In each row you will need to print:

  • n spaces
  • m numbers
  • n spaces

Your job is to figure out n (which will be zero in the last line) and m based on row number.

[This is more like a comment but I needed more formatting options than comments provide]

查看更多
叼着烟拽天下
3楼-- · 2019-01-18 05:23

You can try this code in java. It's simple :)

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();
    }
  }
}
查看更多
该账号已被封号
4楼-- · 2019-01-18 05:24
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

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-18 05:35

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();
}
查看更多
放我归山
6楼-- · 2019-01-18 05:35
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;
        }
    }
}
查看更多
做个烂人
7楼-- · 2019-01-18 05:35
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;
       }




     }
}
查看更多
登录 后发表回答