U E D R , A S I H C RSS

파스칼삼각형/윤종하

Difference between r1.1 and the current

@@ -1,3 +1,41 @@
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):
@@ -18,8 +56,4 @@

n=input("줄 수")
pascal(n)
}}}
 
알고리즘은 쉬운데 파이썬은 개행이 자동인데다가 공백도 자동으로 들어가서 힘드네요 ㅠ
암튼 수학문제는 수학책이 진리인듯요 ㅋ



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.0184 sec