U E D R , A S I H C RSS

파스칼삼각형/강희경

바보같이 문제도 안읽고 코딩하다보니
다른 문제를 풀었네요.
파스칼삼각형을 출력하는 코드입니다.
#include <iostream>
using namespace std;

void printArray(int *arr, int n);  //각 행을 출력하는 함수
void copyArray(int *fArr, int *arr);  //이전의 행을 임시저장하는 함수

int column;

int main(){
	cout << "출력한 파스칼삼각형의 열의 값을 입력해주세요(ex,6).";
	cin >> column;
	if(column > 0){
		int *array = new int[column];  //출력되는 행의 값
		int *foreArray = new int[column];  //이전의(상위의) 행의 값
		for(int i = 0; i<column; i++){
			array[0] = 1;  //행의 처음과 끝은 1
			for(int j = 1; j < column-1; j++){  //숫자는 자신의 머리 위에 있는 2개의 숫자를 더한 값
				array[j] = foreArray[j-1] + foreArray[j];
			}
			array[i] = 1;  //행의 처음과 끝은 1
			printArray(array, i);  //행의 내용을 출력한다.
			copyArray(foreArray, array);  //출력했던 행의 내용을 저장하고 다음행을 위해 초기화해준다.
		}
	}
	else
		cout << "잘못된 입력입니다."<< endl;
	return 0;
}

void printArray(int *arr, int n){
	int blank = (column - n)/2;
	for(int j = 0;j < blank; j++){  //행 출력전의 빈칸 출력
		cout << " ";
	}
	for(int i = 0; i <= n; i++){   //숫자 사이 사이의 빈칸 출력
		cout << arr[i] << " ";
	}
	cout << endl;
}

void copyArray(int *fArr, int *arr){
	for(int i = 0; i < column; i++){
		fArr[i] = arr[i];
		arr[i] = 0;
	}
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:22
Processing time 0.0117 sec