![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Location: Philippines
Posts: 21
Rep Power: 0
![]() |
Newbie prob:how to make combinations without repeatition
About the code in making the numbers from 1-42, in different combinations without repeatition, by 6 numbers, is there any function about that?, hmmmm... i cant find the answer on this... i know im going to use the for loop but i cant find the solution... pls help me hnx
example: collection of numbers from 1-7 answer: 123456 123457 123467 123567 124567 134567 234567 Thanks for helping a newbie prob |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
Use nested for loops, where the start or end conditions of inner loops are determined by the value controlling outer loops.
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
uh, what the hell are you trying to do? "making numbers"? w/o repitition there is the fundamental theorem of arithmetic, which might help you.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Next time, post in the proper forum: "Software design and algorithms".
std::vector<std::vector<value> >
get_combinations(std::vector<value> values, unsigned m)
{
std::vector<std::vector<value> > ret;
unsigned n = values.size();
std::vector<value>::const_iterator* iters =
new std::vector<value>::const_iterator[m];
for(unsigned i=0;i<m;i++)
iters[i] = values.begin() + i;
while(true)
{
unsigned i;
std::vector<value> tmp;
for(unsigned j=0;j<m;j++)
tmp.push_back(*(iters[j]));
ret.push_back(tmp);
for(i=m;i>0;i--)
{
if(++(iters[i-1]) != values.end() - (m - i))
break;
}
if(!i)
break;
for(unsigned j = i;j<m;j++)
{
iters[j] = iters[j-1]+1;
}
}
return ret;
}
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
By the way, that will use ten metric fucktons of memory if you run it with a lot of values. If you needed to find all the combinations of a large set of values, but you didn't need to hold them all in memory at once, it'd be far better to implement as find_first_combination() and find_next_combination() functions.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Make the newbie feel welcome! | TeamElement3 | Community Introductions | 22 | Jul 18th, 2005 12:09 AM |
| Newbie: How to make simple Timer programe | smith000monday | Assembly | 0 | May 17th, 2005 3:54 AM |
| Newbie here... | MuSiG | Visual Basic | 9 | Apr 21st, 2005 8:50 PM |