May 14, 2013 at 6:38pm May 14, 2013 at 6:38pm UTC
Hi, I was thinking for a few days at a C++ problem and I couldn't solve it, I tried but I'm a begginer.
So n and k are two natural numbers with max. five numbers in them. (Example : 11111).
It will need to output the sum of N in K even numbers.
Examples:
For n = 18 and k = 3 the output will be 2+6+10=18
For n = 10 and k = 4 it will say "it's impossible)
English isn't my native language so please be kind.
Thank you!
Last edited on May 14, 2013 at 6:39pm May 14, 2013 at 6:39pm UTC
May 14, 2013 at 7:12pm May 14, 2013 at 7:12pm UTC
What part did you get stuck on? This is basically just one for loop adding to a variable. For i from n to k, if i is even add i to sum.
Last edited on May 14, 2013 at 7:35pm May 14, 2013 at 7:35pm UTC
May 14, 2013 at 7:19pm May 14, 2013 at 7:19pm UTC
If n is odd, then it is impossible. That you can test easily.
Why is n=10, k=4 impossible? No repeats and no 0 allowed?
18/2 = 9
1+2+3 = 6
9-6 = 3
2*1 = 2
2*2 = 4
2*(3+3) = 12
2+4+12 = 18
May 14, 2013 at 7:21pm May 14, 2013 at 7:21pm UTC
I don't get it. Maybe you could write in your native language, and use google translate?
Aceix.
May 14, 2013 at 7:39pm May 14, 2013 at 7:39pm UTC
I misread.
"It will need to output the sum of N in K even numbers ."
n is the number, which has to be even for this to work, and k is how many even numbers you have to use as its sum.
That's why n=10, k=4 is impossible - There is no way with four natural even numbers you can add to 10 (2+4+6+8 is already too high)
I'll do some thinking about how to solve it.
Last edited on May 14, 2013 at 7:41pm May 14, 2013 at 7:41pm UTC
May 14, 2013 at 7:47pm May 14, 2013 at 7:47pm UTC
Ok got it! So do you need tips on how to write a code for that?
Aceix.
May 14, 2013 at 8:03pm May 14, 2013 at 8:03pm UTC
Thank you L B, keskiverto and Aceix!