Permutations and Combinations
一些算法:http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture25.html
1. Print all permutations of a string.
一位网友的博文:http://n1b-algo.blogspot.com/2009/01/string-permutations.html
2. Given a telephone number, find all the permutations of the letters.
char letters[10][5] =
{
" ", //0
" ", //1
"ABC", //2
"DEF", //3
"GHI", //4
"JKL", //5
"MNO", //6
"PQRS", //7
"TUV", //8
"WXYZ" //9
};
int num_letters[10] = {1,1,3,3,3,3,3,4,3,4};
int answer[10];
void print_result(string num)
{
for(int i=0; i<10; i++)
{
cout<<letters[num[i]-'0'][answer[i]];
}
cout<<endl;
}
void num_letter(string num, int level)
{
if(level == 10)
{
print_result(num);
return;
}
for(int i=0; i<num_letters[num[level]-'0']; i++)
{
answer[level]=i;
num_letter(num, level+1);
}
}
浙公网安备 33010602011771号