Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

Task: find the combinations of 5 (non-repeated) numbers from the array such that the sum of them is 250

OpenStudy (anonymous):

#include<iostream> using namespace std; int main (){ int arr[] = {26, 52, 37, 17, 75, 89, 23, 94}; for (int a = 0; a< 8; a++){ for (int b = a+1; b< 8; b++){ if (arr[b] != arr[a]){ for (int c = a+2; c< 8; c++){ if (arr[c] != arr[b] && arr[c] != arr[a]){ for (int d = a+3; d< 8; d++){ for (int e = a+4; e< 8; e++){ if (arr[a]+arr[b]+arr[c]+arr[d]+arr[e] == 250){ cout << arr[a] << " " << arr[b] << " " << arr[c] << " " << arr[d] << " " << arr[e] << endl; break; } } } } } } system("PAUSE"); return 0; }

OpenStudy (anonymous):

It doesn't work pretty well. @AravindG Any ideas?

OpenStudy (anonymous):

The very result of the new version is better (Still not good), but the code is VERY nasty :( int main (){ int arr[] = {26, 52, 37, 17, 75, 89, 23, 94}; for (int a = 0; a< 8; a++){ for (int b = a+1; b< 8; b++){ if (arr[b] != arr[a]){ for (int c = a+2; c< 8; c++){ if (arr[c] != arr[b] && arr[c] != arr[a]){ for (int d = a+3; d< 8; d++){ if (arr[d] != arr[a] && arr[d] != arr[b] && arr[d] != arr[c]){ for (int e = a+4; e< 8; e++){ if (arr[d] != arr[a] && arr[d] != arr[b] && arr[d] != arr[c] ){ if (arr[a]+arr[b]+arr[c]+arr[d]+arr[e] == 250){ cout << arr[a] << " " << arr[b] << " " << arr[c] << " " << arr[d] << " " << arr[e] << endl; break; } } } } } } } } } } system("PAUSE"); return 0; }

ganeshie8 (ganeshie8):

would u like to see perl code for the same ?

OpenStudy (anonymous):

Hmm, the only language I know is cpp, but... please post it if you don't mind!

ganeshie8 (ganeshie8):

ha i use perl cuz its simpler than cpp. below is the code. here ive cheated by using Math module in perl ! so it wont help u in figuring out how to generate combinations of an array. ``` use Math::Combinatorics; my @n = qw(26 52 37 17 75 89 23 94); my $combinat = Math::Combinatorics->new(count => 5, data => [@n], ); while(my @combo = $combinat->next_combination){ my $c=0; foreach my $k (@combo) { $c += $k; } print join " ", @combo if $c == 250; } ```

OpenStudy (anonymous):

*white flag* I don't understand your code </3

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!