EG Information

Main Index
EG Manual
Disclaimer
Legal Information
Hall of Fame
Hall of Shame
Member Rankings
Members List
Meet the Staff

Training Missions

Read Me First New
Basic Skills
Realistic Scenarios
Cryptography
Software Cracking
Linux ELF Binary Cracking
Logical Thinking
Programming
Patching
Steganography
Deface This Wall
/dev/null
/dev/urandom

Knowledge Bank

Discussion Forums
Enigma Chat New
RSS Feeds RSS
Articles / Tutorials
Videos
Online EG MP3 Player Radio
Enigma Zine
Downloads
Tools New

Code Resources

Submit Code
Ajax
ASM
Bash
C
CPP
Csharp
Delphi
Haskell
Java
Javascript
Jython
Lisp
mIRC
MySQL
Perl
PHP
Python
QBASIC
VisualBasic

Hakipedia: An open collaborative for all your information security needs.

The Urinal

Click Here To Vote For EG!

Has Enigma Group Helped You? Then Help Us By Advertising For Us. Place One Of The Following Images On Your Site.

enigma group

enigma group

enigma group

enigma group

Enigma Group's Code Bank


combinatorics algorithms

By: marooned  -  Date Submitted: 2009-08-10 15:14:22

  1. #include <stdio.h>
  2. #define N 5
  3. #define M 3
  4.  
  5. char str[N+1];
  6. char dig[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  7. int counter;
  8. char used[N];
  9.  
  10. //generate binary strings
  11. void bstrings(int k)
  12. {
  13. if (k==-1)
  14. {
  15. printf("%sn", str);
  16. return;
  17. }
  18. str[N-1-k] = '0'; bstrings(k-1);
  19. str[N-1-k] = '1'; bstrings(k-1);
  20. }
  21.  
  22.  
  23. //generate all possible strings of length N on M digits
  24. void strings(int k)
  25. {
  26. int i;
  27.  
  28. if (k==-1)
  29. {
  30. //do something with the string
  31. printf("%d. %sn", ++counter, str);
  32. return;
  33. }
  34.  
  35. for(i=0; i<M; i++)
  36. {
  37. str[N-k-1] = dig[i];
  38. strings(k-1);
  39. }
  40. }
  41.  
  42. //generate all permutations on N digits
  43. void permstrings(int k)
  44. {
  45. int i;
  46.  
  47. if (k==-1)
  48. {
  49. printf("%d. %sn", ++counter, str);
  50. return;
  51. }
  52.  
  53. for(i=0; i<N; i++)
  54. {
  55. //do something with the string
  56. if (!used[i])
  57. {
  58. str[N-k-1] = dig[i];
  59. used[i] = 1;
  60. permstrings(k-1);
  61. used[i] = 0;
  62. }
  63. }
  64. }
  65.  
  66.  
  67.  
  68. int _tmain(int argc, _TCHAR* argv[])
  69. {
  70. permstrings(N-1);
  71. getchar();
  72. return 0;
  73. }
  74.  
Return to c category list

Who's Online

485 Guests, 100 Users
asapong, CollapsingWalls, Diznablo, mehtaparag, bitstrike, jnony, C, Nicid1, Nusquam-Redono-Sapientia, bazcrown, saifulfaizan, The End, Ultraminor, psychomarine, st3alth, themastersinner, pgmrlink, login, lionaneesh, ishkur88, mahraja, Mac, chekifr, gandalf88, Vap0r, t0ast, tantrum6226, BnE, Distorted, Psiber_Syn, Ausome1, invas10n, oldgoat, freedaysbecumei, BinaryShinigami, Rex_Mundi, Red_beard, Strobeflux, s0m3nak3dguy, Descent, teehee, machupicchu, Genetix, Anandarl, NotMyOwn, thegamerdude, Godzila, popo12341234, RedEvolution, rabbidmind, velocity_b, myne17, teto111, aVoid, Central-Gsm, 1101, JackalReborn, Nasrudin, InjectioN, h4lted, c0re, DisPater, markt4death, splatta, Jackowacko, saint556, Pyron2312, Azerion, howsens, white.hat.gone.bad, vazzilly, pwunkz, Ios, Inverted, QuarterCask, Infernoe11, deskata, cr4ck3rj4ck, Blizer, jasonmax, j0sh, gwenwavor, N4g4c3N, spizeyboy, Network X, Uino59, Jae Cee, ianFDK, saykov, medhaavee, zofy, demonkiller410, Stumble, SaMTHG, kishore, Raze, helasraizam, Venom1019, Jakabo, sharpie392