1
、将一整数逆序后放入一数组中(要求递归实现)
void
convert(int *result, int n)
{
if(n>=10)
convert(result+1, n/10);
*result = n%10;
}
int
main(int argc, char* argv[])
{
int n = 123456789, result[20]={};
convert(result, n);
printf("%d:", n);
for(int i=0; i<9; i++)
printf("%d", result[i]);
return getchar();
}
2
、求高于平均分的学生学号及成绩(学号和成绩人工输入)
double
find(int total, int n)
{
int number, score, average;
scanf("%d", &number);
if(number != 0){
scanf("%d", &score);
average = find(total+score, n+1);
if(score >= average)
printf("%d:%d/n", number, score);
return average;
}else{
printf("Average=%d/n", total/n);
return total/n;
}
}
int
main(int argc, char* argv[])
{
find(0, 0);
return getchar();
}
3
、递归实现回文判断(如:abcdedbca就是回文)
int
find(char *str, int n)
{
if(n<=1) return 1;
else if(str[0]==str[n-1]) return find(str+1, n-2);
else return 0;
}
int
main(int argc, char* argv[])
{
char *str = "abcdedcba";
printf("%s: %s/n", str, find(str,
strlen(str)) ? "Yes" : "No");
return getchar();
}
4
、组合问题(从
M
个不同字符中任取
N
个字符的所有组合)
void
find(char *source, char *result, int n)
{
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();
}
7
、随机分配座位,共50个学生,使学号相邻的同学座位不能相邻(
早些时候用
C#
写的,没有用
C
改写)。
static
void Main(string[] args)
{
int Tmp = 0, Count = 50;
int[] Seats = new int[Count];
bool[] Students = new bool[Count];
System.Random RandStudent=new System.Random();
Students[Seats[0]=RandStudent.Next(0,Count)]=true;
for(int i = 1; i < Count; )
{
Tmp=(int)RandStudent.Next(0,Count);
if((!Students[Tmp])&&(Seats[i-1]-Tmp!=1) && (Seats[i-1] - Tmp) != -1){
Seats[i++] = Tmp;
Students[Tmp] = true;
}
}
foreach(int Student in Seats)
System.Console.Write(Student + " ");
System.Console.Read();
}
8
、求网格中的黑点分布(有6*7的网格,在某些格子中有黑点,已知各行与各列中有黑点的点数之和)
#define
ROWS 6
#define
COLS 7
int
iPointsR[ROWS] = {2, 0, 4, 3, 4, 0}; //
各行黑点数和的情况
int
iPointsC[COLS] = {4, 1, 2, 2, 1, 2, 1}; //
各列黑点数和的情况
int
iCount, iFound;
int iSumR[ROWS], iSumC[COLS], Grid[ROWS][COLS];
int
Set(int iRowNo)
{
if
(iRowNo == ROWS){
for(int iColNo=0; iColNo < COLS && iSumC[iColNo]==iPointsC[iColNo]; iColNo++)
if(iColNo == COLS-1){
printf("/nNo.%d:/n", ++iCount);
for(int i=0; i < ROWS; i++)
for(int j=0; j < COLS; j++)
printf("%d%c", Grid[i][j], (j+1) % COLS ? ' ' : '/n');
iFound = 1; // iFound = 1
,有解
}
}else{
for(int iColNo=0; iColNo < COLS; iColNo++)
{
if(iPointsR[iRowNo] == 0){
Set(iRowNo + 1);
}else if(Grid[iRowNo][iColNo]==0){
Grid[iRowNo][iColNo] = 1;
iSumR[iRowNo]++; iSumC[iColNo]++; if(iSumR[iRowNo]<iPointsR[iRowNo] && iSumC[iColNo]<=iPointsC[iColNo])
Set(iRowNo);
else
if(iSumR[iRowNo]==iPointsR[iRowNo] && iRowNo < ROWS)
Set(iRowNo + 1);
Grid[iRowNo][iColNo] = 0;
iSumR[iRowNo]--; iSumC[iColNo]--;
}
}
}
return
iFound; //
用于判断是否有解
}
int
main(int argc, char* argv[])
{
if(!Set(0))
printf("Failure!");
return getchar();
}