-
-
Save andreazevedo/3783432 to your computer and use it in GitHub Desktop.
Dado um vetor com 'n' números distintos, calcular quantas possibilidades existem para se obter a soma 's'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def countSum(numbers: List[Int], expectedResult: Int): Int = { | |
countSumRecursively(numbers, 0, expectedResult); | |
} | |
def countSumRecursively(numbers: List[Int], currentResult: Int, expectedResult: Int): Int = { | |
if (currentResult == expectedResult) | |
1 | |
else if ((currentResult > expectedResult) || (numbers.isEmpty)) | |
0 | |
else | |
countSumRecursively(numbers, currentResult + numbers.head, expectedResult) + countSumRecursively(numbers.tail, currentResult, expectedResult); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment