1题暴力 7
2题找规律 26
3 题DFS深度优先搜索
注意检查是否可以继续搜
答案 1580
#include
#include
#include
const int N = 23, undefined = 0x3f3f3f3f;
int p[N][2] = { { 0, 1 },{ 0, 3 },{ 0, 4 },{ 0, 5 },{ 1, 2 },
{ 1, 4 },{ 1, 5 },{ 1, 6 },{ 2, 5 },{ 2, 6 },
{ 3, 4 },{ 3, 7 },{ 3, 8 },{ 4, 5 },{ 4, 7 },
{ 4, 8 },{ 4, 9 },{ 5, 6 },{ 5, 8 },{ 5, 9 },
{ 6, 9 },{ 7, 8 },{ 8, 9 } };
int cnt, a[10];
bool check() {
for (int i = 0; i < N; ++i) {
if (abs(a[p[i][0]] - a[p[i][1]]) == 1) {
return false;
}
}
return true; //合理
}
bool vis[10];
void dfs(int lv) {
if (lv >= 10) {
if (check()) {
cnt++;
//for (int i = 0; i < 10; ++i) printf("%d ", a[i]);puts("");
}
return;
}
for (int i = 0; i < 10; ++i) if (!vis[i]) {
a[lv] = i;
if (check()) {
vis[i] = true;
dfs(lv + 1);
vis[i] = false;
}
a[lv] = undefined;
}
}
int main()
{
cnt = 0;
memset(vis, 0, sizeof vis);
memset(a, 0x3f, sizeof a);
dfs(0);
printf("%d\n", cnt);
return 0;
}
1580
==============我是分割线=================
4题 基本算法 快速排序
/*--------------------------*/
for (int i = p; i < j; ++i) {
a[i] = a[i + 1];
}
a[j] = x;
/*---------------------------*/
==============我是分割线=================
5 题 考察补码思想吧
(x + 1) & x
6 题 DFS搜索 64
#include
#include
const int N = 12, M = 13, undefined = 0x3f3f3f3f;
int a[N], cnt;
bool vis[M + 1];
bool check() {
return ((a[2] == undefined || a[0] + a[1] == a[2])
&& (a[5] == undefined || a[3] - a[4] == a[5])
&& (a[8] == undefined || a[6] * a[7] == a[8])
&& (a[10] == undefined || a[9] % a[10] == 0)
&& (a[11] == undefined || a[9] / a[10] == a[11]));
}
void dfs(int lv) {
if (lv >= N) {
if (check()) {
cnt++;
//for (int i = 0; i < N; ++i) printf("%d%c", a[i], " \n"[i == N - 1]);
}
return;
}
for (int i = 1; i <= 13; ++i) if(!vis[i]) {
a[lv] = i;
if (check()) {
vis[i] = true;
dfs(lv + 1);
vis[i] = false;
}
a[lv] = undefined;
}
}
int main()
{
memset(vis, 0, sizeof vis);
memset(a, 0x3f, sizeof a);
cnt = 0;
dfs(0);
printf("%d\n", cnt);
return 0;
}
7题 也是搜索(爆搜,也不耗时)116
#include
#include
const int N = 12, undefined = 0x3f3f3f3f;
int mp[5][5];
int dir[][2] = { 0, 1, -1, 0, 0, -1, 1, 0 };
void dfs(int x, int y) {
mp[x][y] = 2;
for (int i = 0; i < 4; ++i) {
int xx = x + dir[i][0], yy = y + dir[i][1];
if (xx >= 1 && xx <= 3 && yy >= 1 && yy <= 4 && mp[xx][yy] == 1) {
dfs(xx, yy);
}
}
}
bool check(int i, int j, int k, int m, int n) {
memset(mp, 0, sizeof mp);
int a[5] = { i, j, k, m, n };
for (int i = 0; i < 5; ++i) {
int x = a[i] / 4 + 1 - (a[i] % 4 == 0), y = a[i] % 4 == 0 ? 4 : a[i] % 4;
if (mp[x][y] == 1) return false;
else mp[x][y] = 1;
}
int cnt = 0;
for (int i = 1; i <= 3; ++i) {
for (int j = 1; j <= 4; ++j) if(mp[i][j] == 1) {
dfs(i, j);
cnt++;
}
}
//输出每一种情况
//if (cnt == 1) {
// for (int i = 0; i < 3; ++i) {
// for (int j = 1; j <= 4; ++j)
// printf("%2d%c", mp[i + 1][j] != 0 ? i * 4 + j : 0, " \n"[j == 4]);
// }
// puts("===============");
//}
return cnt == 1;
}
int main()
{
int cnt = 0;
//一下循环,保证没有重复。因为i < j < k < m < n
for (int i = 1; i <= 8; ++i) {
for (int j = i + 1; j <= 12; ++j) {
for (int k = j + 1; k <= 12; ++k) {
for (int m = k + 1; m <= 12; ++m) {
for (int n = m + 1; n <= 12; ++n) {
cnt += check(i, j, k, m, n);
}
}
}
}
}
printf("%d\n", cnt);
return 0;
}
8题 四平方和
#define _CRT_SECURE_NO_WARNINGS
#include
#include
void work(int n) {
int R = sqrt(n + 0.5);
for (int i = 0; i <= R; ++i) {
int sum0 = i * i;
for (int j = 0; j <= R; ++j) {
int sum1 = sum0 + j * j;
if (sum1 > n) break;
for (int k = 0; k <= R; ++k) {
int sum2 = sum1 + k * k;
if (sum2 > n) break;
for (int m = 0; m <= R; ++m) {
int sum3 = sum2 + m * m;
if (sum3 > n) break;
if (sum3 == n) {
printf("%d %d %d %d\n", i, j, k, m);
return;
}
}
}
}
}
}
int main()
{
int n(0);
scanf("%d", &n);
work(n);
return 0;
}