1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Combinaisons
{
class Program
{
static void Main( string[] args )
{
Combinaisons1();
Combinaisons2();
}
static void Afficher( string type, List<string> chaines, List<string> resultat )
{
Console.WriteLine( type + " pour les chaines :" );
foreach ( string chaine in chaines )
{
Console.WriteLine( " " + chaine );
}
Console.WriteLine( "Résultat :" );
foreach ( string chaine in resultat )
{
Console.WriteLine( " " + chaine );
}
Console.WriteLine();
}
static void Combinaisons1()
{
string s1 = "ABC";
string s2 = "DEF";
string s3 = "GHI";
List<string> chaines = new List<string>();
chaines.Add( s1 );
chaines.Add( s2 );
chaines.Add( s3 );
List<string> resultat = Combinaisons1( chaines );
Afficher( "Combinaisons1", chaines, resultat );
}
// toutes les chaines de "chaines" ont la meme taille
static List<string> Combinaisons1( List<string> chaines )
{
List<string> resultat = new List<string>();
int tailleChaine = chaines[0].Length;
int nombreDeChaines = chaines.Count;
int index = 0;
int[] indices = new int[tailleChaine];
indices.Initialize();
char[] chaineGeneree = new char[tailleChaine];
bool continuer = true;
while ( continuer )
{
for ( int i = index ; i < tailleChaine ; i++ )
{
chaineGeneree[i] = chaines[indices[i]][i];
}
resultat.Add( new string( chaineGeneree ) );
index = Suivant1( indices, nombreDeChaines );
continuer = (index != -1);
}
return resultat;
}
static int Suivant1( int[] indices, int nombre )
{
int index = indices.Length - 1;
while ( index >= 0 )
{
indices[index]++;
if ( indices[index] == nombre )
{
indices[index] = 0;
index--;
}
else
return index;
}
return -1;
}
static void Combinaisons2()
{
string s1 = "ABC";
string s2 = "DEF";
string s3 = "GHI";
List<string> chaines = new List<string>();
chaines.Add( s1 );
chaines.Add( s2 );
chaines.Add( s3 );
List<string> resultat = Combinaisons2( chaines );
Afficher( "Combinaisons2", chaines, resultat );
}
static List<string> Combinaisons2( List<string> chaines )
{
List<string> resultat = new List<string>();
int tailleChaine = chaines[0].Length;
int nombreDeChaines = chaines.Count;
int index = 0;
int[] indices = new int[nombreDeChaines];
indices.Initialize();
char[] chaineGeneree = new char[nombreDeChaines];
bool continuer = true;
while ( continuer )
{
for ( int i = index ; i < nombreDeChaines ; i++ )
{
chaineGeneree[i] = chaines[i][indices[i]];
}
resultat.Add( new string( chaineGeneree ) );
index = Suivant2( indices, chaines );
continuer = (index != -1);
}
return resultat;
}
static int Suivant2( int[] indices, List<string> chaines )
{
int index = indices.Length - 1;
while ( index >= 0 )
{
indices[index]++;
if ( indices[index] == chaines[index].Length )
{
indices[index] = 0;
index--;
}
else
return index;
}
return -1;
}
}
} |