|
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream.h>
#include <vector.h>
#include <conio.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
void Calculate( int i );
vector<int> pyra[100];
int main(int argc, char* argv[])
{
cout <<" ***** Display Pascal Triangle *****";
cout <<"\n\nInput the Line Number : ";
int LineNum;
cin >> LineNum;
for(int i=0 ; i<LineNum ; i++)
{
Calculate(i);
}
for(int i=0 ; i<LineNum ; i++)
{
for( int x=0 ; x<i+1 ; x++)
{
cout<<pyra[i].at(x)<<"\t";
}
cout<<"\n";
}
getch();
return 0;
}
//---------------------------------------------------------------------------
void Calculate(int LineNum)
{
if( LineNum == 0 )
{
pyra[0].push_back(1);
}
else
{
pyra[LineNum].push_back(pyra[LineNum-1].at(0));
for(int i=0; i< LineNum -1 ; i++)
{
pyra[LineNum].push_back( pyra[LineNum-1].at(i) + pyra[LineNum-1].at(i+1) );
}
pyra[LineNum].push_back(pyra[LineNum-1].at(LineNum-1));
}
}
//-----------------------------------------------------------------------------
빌더에서 작업한 것이어서 Bc++ 3.1에서는 안돌아 갈수 있음다.
박준호 님이 쓰신 글 :
: 1
: 1 1
: 1 2 1
: 1 3 3 1
: 1 4 6 4 1
:
: 예를 들어 숫자 5를 넣으면 위와같이 되는결과를 내는것인데여...
: 솔직히 레포트 맞습니다. 그런데 제가 문제를 풀긴 했는데.. ㅡ.ㅡ
:
: 고수님들의 좋은 방법이 더 있지 않을까해서 물어보는겁니다.
: 알고리즘, 자료구조 무지 어렵네여.. 고수님들의 좋은 방법 부탁드립니다.
:
: 그리고 밑에는 제가 구현한것입니다.
: #include<stdio.h>
:
: int sum_total(int n)
: {
: int i, sum = 0;
:
: for (i = 1; i <= n; i++)
: sum = sum + i;
:
: return sum;
: }
:
: void main()
: {
: int array[100];
: int i, j, stdLeft, stdRight, index, line, maxLine;
: line = 1;
: index = 0;
:
: scanf("%d", &maxLine);
:
: if (maxLine > 100)
: {
: printf("숫자가 너무 큽니다. 100 이하로 해주십시요");
: exit(1);
: }
:
: for (i = 0; i < 100; i++)
: array[i] = 0;
:
: while (line <= maxLine)
: {
: for(i = 0; i < (maxLine - line); i++)
: {
: printf(" ");
: }
: for(j = 1; j <= line; j++)
: {
: stdLeft = sum_total(line - 1);
: stdRight = (stdLeft + line) - 1;
:
: if ((index - line) <= 0)
: array[index] = 1;
: else if (stdLeft == index)
: {
: array[index] = 1;
: }
: else if (stdRight == index)
: {
: array[index] = 1;
: }
: else
: array[index] = array[index - line] + array[index - line + 1];
:
: if (array[index] < 10)
: {
: printf(" ");
: }
:
: printf("%d", array[index]);
: printf(" ");
:
: index++;
: }
:
: printf("\n");
: line++;
: }
: }
:
: 너무 지저분한것 같네요.. ㅜ,ㅜ
: 한마디로 그냥 기능만 되는.. ㅡ.ㅡ
|