📄 c题目答案.txt
字号:
char temp = str[a];
str[a] = str[b];
str[b] = temp;
printf("%s\n", str);
}
void main()
{
int i ,j, end = 0;
char str[N+1];
for(i = 0; i < N/2; i++)
str[i] = 'A';
str[i++] = 'O';
for(; i < N; i++)
str[i] = 'B';
str[N] = 0;
printf("%s\n", str);
for(i = N/2; i < N-1; i++)
swapc(i, i+1, str);
for(i = N/2; i < N-1; i++)
{
for(j = i; j > end; j–)
swapc(j, j-1, str);
end++;
}
for(i = N-2; i >= N/2; i–)
swapc(i, i+1, str);
}
//////////////////////////////////////////////////////////////////////////////
//
// 20. (N皇后) 在国际象棋的棋盘上放置N个皇后,使其不能互相攻击,即任意
// 两个皇后不能处在棋盘的同一行,同一列,同一斜线上,试问共有多少种摆法?
//////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "conio.h"
#define N 8
static int a[N][N], count;
bool legal(int row, int col)
{
int i, j;
for(i = 0; i < row; i++)
if(a[i][col])
return false;
for(j = 0; j < col; j++)
if(a[row][j])
return false;
for(i = row-1, j = col-1; (i>=0) &&(j>=0); i–, j–)
if(a[i][j])
return false;
for(i = row-1, j = col+1; (i>=0) &&(j<N); i–, j++)
if(a[i][j])
return false;
return true;
}
void trial(int row)
{
int p, col;
if(row == N)
{
++count;
printf("********%d*********\n", count);
for(p = 0; p < N; p++)
{
for(col = 0; col < N; col++)
printf("%4d", a[p][col]);
printf("\n");
}
}
else
{
for(col = 0; col < N; col++)
{
a[row][col] = 1;
if(legal(row, col))
trial(row+1);
a[row][col] = 0;
}
}
}
void main()
{
trial(0);
}
#include "stdio.h"
#include "conio.h"
#define N 8
static int a[N][N], count, row;
bool legal(int row, int col)
{
int i, j;
for(i = 0; i < row; i++)
if(a[i][col])
return false;
for(j = 0; j < col; j++)
if(a[row][j])
return false;
for(i = row-1, j = col-1; (i>=0) &&(j>=0); i–, j–)
if(a[i][j])
return false;
for(i = row-1, j = col+1; (i>=0) &&(j<N); i–, j++)
if(a[i][j])
return false;
return true;
}
void main(int row)
{
printf("\n%4d\n", row);
int p, col;
if(row == N+1)
{
++count;
printf("********%d*********\n", count);
for(p = 1; p <= N; p++)
{
for(col = 0; col < N; col++)
printf("%4d", a[p][col]);
printf("\n");
}
_getch();
}
else
{
for(int col = 0; col < N; col++)
{
a[row][col] = 1;
if(legal(row, col))
main(row+1);
a[row][col] = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////
//
//21. 请设计一个程序,由计算机把1.. ̄.8的八个自然数填入图中,使得横、
// 竖、对角任何两个相邻的小方格中的两个数是不连续的。(下图右侧的 4 个图
// 为禁止的情形).
//
// ┌─┐ ┌─┐ ┌─┐
// │ │ │4│ │8│
// ┌─┼─┼─┐ └─┼─┐ ┌─┼─┘
// │ │ │ │ │5│ │7│
// ├─┼─┼─┤ └─┘ └─┘
// │ │ │ │ ┌─┐
// └─┼─┼─┘ │6│ ┌─┬─┐
// │ │ ├─┤ │1│2│
// └─┘ │7│ └─┴─┘
// └─┘
///////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "string.h"
#include "math.h"
int a[4][3];
int count = 0;
bool legalelem(int row, int col)
{
switch (row)
{
case 0:
case 3:
if(col == 1)
return true;
else
return false;
case 1:
default:
return true;
}
}
bool islegal(int row, int col)
{
int i, j;
for(i = row-1; i <= row+1; i++)
{
if(i < 0 || i > 3)
continue;
for(j = col-1; j <= col+1; j++)
{
if(j < 0 || j > 2)
continue;
if(abs(a[i][j] - a[row][col]) == 1)
return false;
}
}
return true;
}
void trace(int n)
{
int i, j;
if(n > 8)
{
++count;
printf("\n********** %d **********\n", count);
for(i = 0; i < 4; i++)
{
for(j = 0; j < 3; j++)
{
if(legalelem(i, j))
printf("%6d", a[i][j]);
else
printf("%*s", 6, "");
}
putchar(10);
}
return;
}
for(i = 0; i < 4; i++)
{
for(j = 0; j < 3; j++)
{
if(a[i][j] == -1 && legalelem(i, j))
{
a[i][j] = n;
if(islegal(i, j))
trace(n+1);
a[i][j] = -1;
}
}
}
}
void main()
{
int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < 3; j++)
a[i][j] = -1;
trace(1);
}
///////////////////////////////////////////////////////////////////////////
// 22. 在一个4*4的小方格(如图所示)中放置8个*号,使得每行每列放且
// 仅放两个*号。
// ┌─┬─┬─┬─┐
// │*│*│ │ │
// ├─┼─┼─┼─┤
// │*│ │*│ │
// ├─┼─┼─┼─┤
// │ │*│ │*│
// ├─┼─┼─┼─┤
// │ │ │*│*│
// └─┴─┴─┴─┘
//
// 求出所有的基本解。
//////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#define N 4
static int a[N][N], count;
bool legal(int row, int col)
{
int sum, p;
for(sum = 0, p = 0; p < row; p++)
sum += a[p][col];
if(sum < 2)
{
for(sum = 0, p = 0; p < col; p++)
sum += a[row][p];
if(sum < 2)
return true;
else
return false;
}
else
return false;
}
void trial(int row)
{
if(row == N)
{
++count;
printf("******** %d ********\n", count);
for(int p = 0; p < N; p++)
{
for(int q = 0; q < N; q++)
printf("%4d", a[p][q]);
printf("\n");
}
}
else
{
for(int j1 = 0; j1 < N-1; j1++)
{
a[row][j1] = 1;
if(legal(row, j1))
{
for(int j2 = j1+1; j2 < N; j2++)
{
if(!a[row][j2])
{
a[row][j2] = 1;
if(legal(row, j2))
trial(row+1);
a[row][j2] = 0;
}
}
}
a[row][j1] = 0;
}
}
}
void main()
{
trial(0);
}
//////////////////////////////////////////////////////////////////////////////
//
// 38. 有一集合中有 N 个元素,每个元素均为自然数。给定一个 total (假设每个
// 元素值均小于total),求满足条件的所有子集,子集中各元素之和应等于total。
//////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "string.h"
int a[] = {1, 2, 3, 4, 7, 10};
const int N = sizeof(a)/sizeof(int);
char print[30], temp[5];
void trace(int i,int j, int a[], int b[], int total)
{
if(i == N) //not "i > N"
{
int sum = 0, p;
for(p = 0; p < i-1; p++)
sum += b[p];
if(sum == total)
{
memset(print, 0, sizeof(print));
strcat(strcat(print, itoa(total, temp, 10)), "= ");
for(p = 0; p < i-1; p++)
{
if(b[p])
strcat(strcat(print, itoa(b[p], temp, 10)), " +");
}
memset(print+strlen(print)-1, 0, 2);
printf("%s\n", print);
}
}
else
{
b[j] = a[i];
trace(i+1, j+1, a, b, total);
b[j] = 0;
trace(i+1, j, a, b, total); //not "trace(i+1, j+1, a, b, total);"
}
}
void main()
{
int *b = new int[N], total;
memset(b, 0, sizeof(a));
printf("a[%d] = {", N);
for(int i = 0; i < N; i++)
printf(" %d,", a[i]);
printf("}\ninput the total, insure its value larger than each element of array a:");
scanf("%d", &total);
trace(0, 0, a, b, total);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -