6 Off: Output Pascal's Triangle

mission details

Topic Description: Do you remember when the school learned of Pascal's triangle? Specific definition is not described here, you can refer to the following graphic:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Programming, Pascal's Triangle output before nthe line, requiring input from the keyboard nvalues.

Entry

From the keyboard input nvalues.

Export

Print out before Pascal's Triangle graphic nlines. See section entitled description format. Behind each integers separated by a space to integers

Programming requirements

The tips on the right supplemental code editor.

Programming Tips

Suppose array called a, the array element output format suggested in the following format:

Console.Write("{0} ",a[i,j]);

test introduction

Platform code you write will be tested:

Test input:

4

Expected output:

1

1 1

1 2 1

1 3 3 1

Test input:

6

Expected output:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1


Start your mission, I wish you success!

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ch706
{
    class Program
    {
        static void Main(string[] args)
        {
			/******begin*******/
			int n = Convert.ToInt32(Console.ReadLine());
            int[,] a = new int[n, n];
            
            for (int i = 0; i < a.GetLength(0); ++i)
            {
                for (int j = 0; j <= i; ++j)
                {
                    if(j == 0 || i == j)
                    {
                        a[i, j] = 1;
                    }
                    else
                    {
                        a[i,j] = a[i - 1,j] + a[i-1,j-1];
                    }
                    Console.Write(a[i,j].ToString() + " ");
                }
                Console.WriteLine();
            }

			
			
			/*******end********/

        }
    }
}

  

Guess you like

Origin www.cnblogs.com/mjn1/p/12452193.html