There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie wrote: 1 × 2 + 3 × 4 ≠ 20
John dev wrote:Assume all the operators will have same precedence.
John dev wrote:Write a Program which takes ‘n’ non-zero Integer arguments where n>3.
You need to use n-1 arguments in the same order to have the result as the nth argument by applying any of the arithmetic operators [ + , - , * , / ]. Assume all the operators will have same precedence.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Sounds like one of the more advanced Project Euler problems, or possibly a Java version of those Vorderman 'Countdown' questions.
John dev wrote:
Write a Program which takes ‘n’ non-zero Integer arguments where n>3.
You need to use n-1 arguments in the same order to have the result as the nth argument by applying any of the arithmetic operators [ + , - , * , / ].
Assume all the operators will have same precedence.
Example 1:
Given n = 5 numbers are
1 2 3 4 20
Solution has to be provided in such a way that output should be as below
1 * 2 + 3 * 4 = 20
Campbell Ritchie wrote:I never saw that. I still think it is difficult.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Myke Enriq wrote:Brute force would mean 4 to the power n-1 combinations.
(...)
John dev wrote:
Write a Program which takes ‘n’ non-zero Integer arguments where n>3.
You need to use n-1 arguments in the same order to have the result as the nth argument by applying any of the arithmetic operators [ + , - , * , / ].
Assume all the operators will have same precedence.
Example 1:
Given n = 5 numbers are
1 2 3 4 20
Solution has to be provided in such a way that output should be as below
1 * 2 + 3 * 4 = 20
"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
I hate signatures!
Piet Souris wrote:Nice recursion!
Problem here is, I think, the division. You must use doubles (integer division would not work, of course) and so you could introduce
rounding errors. Therefore you must use rounding in the two parameter "canMake".
Piet Souris wrote:Problem here is, I think, the division. You must use doubles (integer division would not work, of course) and so you could introduce
rounding errors. Therefore you must use rounding in the two parameter "canMake".
Number of elements | Time to find a solution(ms) | Worst case scenario |
---|---|---|
1 | 0 | 0 |
2 | 0 | 0 |
3 | 0 | 0 |
4 | 0 | 1 |
5 | 0 | 00 |
6 | 0 | 3 |
7 | 13 | 25 |
8 | 6 | 12 |
9 | 2 | 3 |
10 | 5 | 11 |
11 | 1 | 37 |
12 | 5 | 164 |
13 | 114 | 520 |
14 | 62 | 2123 |
15 | 110 | 6710 |
16 | 0 | 33705 |
17 | 656 | 109019 |
18 | 109203 | 517168 |
19 | 190 | ... |
aleks Maleks wrote:thanks it works
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |