views:

354

answers:

4

Hi Guys , I try To find a solution to a question .... we have a number , example : 20 ... and we have 6 number :{ a ,b , c , d , e , f} < 20 , t try to find all values of these numbers , but only if we can combinate (whit + or -) whit 2 of this numbers and getting all the value below to 20 : for example

we choose 31 :

a = 22 b = 21 c = 14 d = 11 e = 9 f = 5

we have : 22 - 21 = 1 ; 11 - 9 = 2 ; 14 - 11 = 3 ; 9 - 5 = 4 ; f = 5 ; 11 - 5 = 6 ; 21 - 14 = 7 ; .... .... .... .... .... 21 + 9 = 30 ; 9 + 22 = 31 ;

+6  A: 

This appears like homework, so I'll try to just give you some hints.

You want to loop over all combinations of two numbers from the array. To do that, you can use an outer loop that loops over all the numbers and then inside that loop, an inner loop that also loops over all the numbers.

Depending on exactly what you want to do, you need to decide if you want to handle x, y separately from y, x

R Samuel Klatchko
A: 

No . I Swear , That's Not A homework . If You can give me the code with any language

If it's not homework - what is it then? I'm curious, that's all.
chibacity
The what is it (for) ?
Henk Holterman
Not sure, but apparently it makes you swear.
chibacity
A: 
void FindCombinations(int n, int [] values)
{
   for(int i = 0; i < values.length; i++)
   {
     int left = values[i];
     for(int j = 0; k < values.length; j++)
     {
       int right = values[j];

       if (left == right)
          continue;

       if (left + right < n)
          Console.Write(string.Format("{0} + {1} = {2} < {3}", left, right, left+right, n);
       if (left - right < n)
          Console.Write(string.Format("{0} - {1} = {2} < {3}", left, right, left-right, n);
     }
   }
}

While this question stinks of homework it's not really that hard (although arguably hard to interpret).

Note: This solution is O(n^2) and is not efficient, please keep this in mind.

Aren
You should probably be ignoring the combinations where `i == j`.
R Samuel Klatchko
Hrm you're probably right!
Aren
-1 for not attempting to understand the problem before posting code.
Daniel Trebbien
+1  A: 

Given a natural number n, find all combinations of 6 positive integers a, b, c, d, e, and f such that {a, b, c, d, e, f} ∪ {a + b, a + c, ..., a + f, b + c, b + d, ..., b + f, ..., e + f} ∪ {a - b, a - c, ..., a - f, b - c, b - d, ..., b - f, ..., e - f, b - a, c - a, ..., f - a, c - b, d - b, ..., f - b, ..., f - e} contains 1, 2, 3, 4, ..., n - 1, and n.

I haven't thought about this problem very much, but it appears to me to be a hard problem, indeed. Maybe there is a trick, I am not sure, but if I had to solve this problem, I would first try to find the least n for which there are not infinitely-many solutions (if n is 1, for example, then any combination of 6 natural numbers containing two consecutive natural numbers will work).

I think that you will have better luck with finding a solution to this problem in a mathematics discussion forum.

Daniel Trebbien