Skip to main content
GameDev.net gamedev.net
🔒 Locked

Number Algorithm

Started by Aphelion Feb 16, 2003 at 11:45 AM 1 replies 500+ views
Original Post
Aphelion
Aphelion
Hey, I was just curious how I''d go about creating a few loops that would go through every possible combonation of ''n'' numbers, where order does matter and numbers can be repeated. i.e. - GenerateNumberListFunction(2,2) would return: 1 1 2 2 1 2 1 2 Any thoughts? Thanks, Jesse
| The Hitchhiker''s Guide to Programming |"That is not dead which can eternal lie,And with strange aeons even death may die."-H.P. Lovecraft
Simage
Simage
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
printf("%d\t%d\n",i,j);


should do it...

[edited by - Simage on February 16, 2003 1:19:35 PM]
Fruny
Fruny
C++ has next_permutation and prev_permutation functions that do exactly what you want (generate permutations).

Note that duplicate sequences may be generated if duplicate values exist.

Consult your favourite C++ reference for further details.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.