{
if(n==1){
while(*source)
printf("%s%c\n", result, *source++);
}else{
int i, j;
for(i=0; source[i] != 0; i++);
for(j=0; result[j] != 0; j++);
for(; i>=n; i--)
{
result[j] = *source++;
result[j+1] = '\0';
find(source, result, n-1);
}
}
}
int main(int argc, char* argv[])
{
int const n = 3;
char *source = "ABCDE", result[n+1] = {0};
if(n>0 && strlen(source)>0 && n<=strlen(source))
find(source, result, 3);
return getchar();
}
5、分解成质因数(如435234=251*17*17*3*2)
void prim(int m, int n)
{
if(m>n){
while(m%n != 0) n++;
m /= n;
prim(m, n);
printf("%d*", n);
}
}
int main(int argc, char* argv[])
{
int n = 435234;
printf("%d=", n);
prim(n, 2);
return getchar();
}
6、寻找迷宫的一条出路(o:通路; X障碍)
#define MAX_SIZE 8
int H[4] = {0, 1, 0, -1};
int V[4] = {-1, 0, 1, 0};
char Maze[MAX_SIZE][MAX_SIZE] = {{'X','X','X','X','X','X','X','X'},
{'o','o','o','o','o','X','X','X'},
{'X','o','X','X','o','o','o','X'},
{'X','o','X','X','o','X','X','o'},
{'X','o','X','X','X','X','X','X'},
{'X','o','X','X','o','o','o','X'},
{'X','o','o','o','o','X','o','o'},
{'X','X','X','X','X','X','X','X'}};
void FindPath(int X, int Y)
{
if(X == MAX_SIZE || Y == MAX_SIZE){
for(int i = 0; i < MAX_SIZE; i++)
for(int j = 0; j < MAX_SIZE; j++)
printf("%c%c", Maze[i][j], j < MAX_SIZE-1 ? ' ' : '\n');
}else for(int k = 0; k < 4; k++)
if(X >= 0 && Y >= 0 && Y < MAX_SIZE && X < MAX_SIZE && 'o' == Maze[X][Y]){
Maze[X][Y] = ' ';
FindPath(X+V[k], Y+H[k]);
Maze[X][Y] ='o';
}
}
int main(int argc, char* argv[])
{
FindPath(1,0);
return getchar();
}