unsigned int sum(unsigned int lowerBound, unsigned int upperBound, int row, int col){ clearVisited(); return recursiveSum(lowerBound, upperBound, row, col); }
unsigned int recursiveSum(unsigned int lowerBound, unsigned int upperBound, int row, int col){ if(row > 0 && row <= _rows && col > 0 && col <= _cols && !_visited[row * col]){ _visited[(row * col) + col] = true; unsigned int val = _values[(row * col) + col]; if(val > lowerBound && val < upperBound) return val + recursiveSum(lowerBound, upperBound, row-1, col) + recursiveSum(lowerBound, upperBound, row+1, col) + recursiveSum(lowerBound, upperBound, row, col-1) + recursiveSum(lowerBound, upperBound, row, col+1); }else return 0; }
int recursiveSum(int x){ //Base Case if(x <= 0){ return 0; } //Recursive return recursiveSum(x-1) + x; }
int main(){ int x = foo( 3 ); int y = power2(x); int z = power4(x); int a = recursiveSum(x); int b = addXY(x, y); printf("x = %d \n", x); printf("x squared = %d \n", y ); printf("sum from 0 to %d = %d \n", x, getSum(x)); printf("x^4 = %d \n", z ); printf("sum from 0 to %d = %d \n", x, a); printf("%d + %d = %d \n", x, y, b); return 0; }
unsigned int count(unsigned int lowerBound, unsigned int upperBound){ clearVisited(); int sum = 0, _count = 0; for (int rows = 0 ; rows <= _rows; rows++){ for(int cols = 0; cols <= _cols; cols++){ sum = recursiveSum(lowerBound, upperBound, rows, cols); if (sum > 0) _count++; } } return _count; }
// 필수 조건: n >= 1 // 결과: 1부터 n까지의 합을 반환한다. int recursiveSum(int n) { if(n == 1) return 1; // 더이상 쪼개지지 않을 때 return n + recursiveSum(n-1); }