U E D R , A S I H C RSS

파스칼삼각형/윤종하

C++
#include<iostream>

using namespace std;

int fac(int n)
{
	if(n==0) return 1;
	else return n*fac(n-1);
}

int combi(int n,int k)
{
	return fac(n)/(fac(k)*fac(n-k));
}

int main()
{
	cout<<"몇 줄?";
	int line;
	cin>>line;

	int i,j;

	for(i=0;i<line;i++)
	{
		for(j=0;j<line-i;j++)	cout<<" ";
		for(j=0;j<=i;j++)	cout<<combi(i,j)<<" ";
		cout<<endl;
	}
	
	return 0;
}


- Python
def fac(n):
    if(n==0):
        return 1
    else:
        return n*fac(n-1)

def combi(n,k):
    return fac(n)/(fac(k)*fac(n-k))

def pascal(n):
    for i in range(0,n,1):
        for j in range(0,n-i,1):
            print "",
        for j in range(0,i+1,1):
            print combi(i,j),
        print "\n"

n=input("줄 수")
pascal(n)
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:23
Processing time 0.0128 sec