Computer Science/Algorithm

[Algorithm/C] BOJ.2775 부녀회장

재오니소스 2017. 11. 22. 20:07


접근 방법

1. 0층이 존재한다는것에 주의해야한다. (이 오류를 발견못해서 한참걸렸음)

2. 0층과 1호를 제외한 규칙을 찾는다. (왼쪽 더하기 위)


#include <stdio.h>

#include <stdlib.h>


int main(int argc, char* argv[])

{

int inputCase;

int *num;

int i,j,row,col,a,b,c,d;


scanf("%d",&inputCase); 

num = (int*) malloc(sizeof(int) * inputCase);


for(c=0; c<inputCase ; c++)

{

int inputRow,inputCol;

int **arr;

scanf("%d",&inputRow); scanf("%d",&inputCol);

/*사용자의 입력에 따른 2차원 배열 동적할당*/

arr = (int**) malloc(sizeof(int*) * (inputRow+1)); //여기서 주의해야 함 0층 처리

for(i=0; i<=inputRow; i++)

{

arr[i] = (int*) malloc(sizeof(int) * inputCol);

}

/*****************************************/


for(a=0; a<inputCol; a++)

{

arr[0][a]=a+1;

}


for(b=0; b<=inputRow; b++)

{

arr[b][0]=1;

}


for(row=1; row<=inputRow; row++)

{

for(col=1; col<inputCol; col++)

{

arr[row][col] = arr[row][col-1] + arr[row-1][col];

}

}

num[c] = arr[inputRow][inputCol-1];


for(j=0; j<=inputRow; j++)

{

free(arr[j]);

}

free(arr);

}


for(d=0; d<inputCase; d++)

{

printf("%d\n",num[d]);

}


free(num);

return 0;

}


문제, 사진출처 - https://www.acmicpc.net/problem/2775