U E D R , A S I H C RSS

파스칼삼각형/임상현

후기

  • 그냥 생각나는데로 짰습니다.

소스

#include<iostream>

using namespace std;

int factorial(int a);

int Pascal(int row, int col);

void main(){
	
	int row = 0, col = 0;
	do{
		cin>>row;
		cin>>col;

		if(Pascal(row,col) == -1){
			cout<<"잘못 누르셨습니다. 다시 선택해 주세요."<<endl;	
		}

		else{
			cout<<"파스칼의 삼각형 "<<row<<"행 "<<col<<"열의 숫자는 "<<Pascal(row,col)<<"입니다."<<endl;
		};
	}while(Pascal(row,col) == -1);

}

int factorial(int a){
	int output= 1;

	if( a == 0 ){
		return output;
	}
	
	else{
		for( ; a> 0 ; a--){
			output *= a;
		}
		return output;
	}
}

int Pascal(int row, int col){
	if(row == col || col == 1){
		return 1;
	}
	else if( row < col ){
		return -1;
	}
	else{
		return factorial(row-1) / ( factorial(col-1) * factorial(row - col) );
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:23
Processing time 0.0077 sec