我要睡觉了,今天还有自己的事,先将局部代码传上来,大家可以看看是否对你的开发有所启发:
[CODE]
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class MyCounter
{
private:
enum{N = 10800};
char counter[N];
public:
MyCounter()
{
reset();
}
void reset()
{
for(int i = 0; i<N; i++)
{
counter[i] = 0;
}
}
void display()
{
int i = 0;
while(i<N && (int)counter[i]==0)
{
i++;
}
int first = i;
if(first == N)
cout<<"0";
else
cout<<(int)counter[first];
for(i = first+1 ; i<N; i++)
{
int n = (int)counter[i];
if(n == 0)
cout<<"00";
else if(n<10)
cout<<'0'<<n;
else
cout<<n;
}
}
void operator++()
{
for(int i = N-1; i>=0; i--)
{
int num = (int)counter[i];
if(num<99)
{
counter[i] = ++num;
break;
}
else
{
counter[i] = 0;
continue;
}
}
}
};
class LatinMatrix
{
private:
int n; // to indicate how many numbers we have
string * lmStr; // an string array to save all the init numbers
vector<string *>strPerm; // to save the all combination of numbers
int * pos; // pos[0] indicates the current position of the first line,
// pos[1] indicates the current position of the second line, and so on
MyCounter myCounter; // to save the count of LatinMatrix
public:
static bool findSameChar(const char * t)
{
string temp;
int length = strlen(t);
for(int i = 0; i<length; i++)
temp += t[i];
for(i = 0; i<length; i++)
{
int pos = temp.find(t[i]);
if(pos != i)
return true;
}
return false;
}
static void integerToString(string & str, const int & t)
{
ostringstream oss;
oss << t;
str = oss.str();
}
LatinMatrix(int n)
{
this->n = n;
lmStr = new string[n];
pos = new int[n];
string temp;
for(int i = 0; i<n; i++)
{
pos[i] = 0; // init all line's current position to 0
integerToString(temp, i+1);
lmStr[i] = string(temp);
}
sort(lmStr, lmStr+n);
strPerm.push_back(lmStr);
while(next_permutation(lmStr, lmStr+n))
{
string * newLmStr = new string[n];
for(int j = 0; j<n; j++)
{
newLmStr[j] = lmStr[j];
}
strPerm.push_back(newLmStr);
}
}
void trytoFindLatinMatrix() // when I find a LatinMatrix, I print the matrix and increment the count
{
// the method to find the LatinMatrix is:
// first I get the first string array
// and then I get the second string array
// and then I check, whether this second string array is valid,
// when yes, then go with the third string array
// otherwise, I go a step back,
// something must be updated within this process, this is the pos value
// pos records the value of string array of permutation,
// so that I can follow the trace of the process
}
~LatinMatrix()
{
delete [] lmStr;
delete [] pos;
}
void display()
{
for(int i = 0; i<strPerm.size(); i++)
{
string * pStr = strPerm[i];
for(int j = 0; j<n; j++)
{
cout<<pStr[j]<<" ";
}
cout<<endl;
}
}
};
int main()
{
LatinMatrix lm(5);
lm.display();
return 0;
}
[/CODE]