# 第七届蓝桥杯A组C

1.网友年龄

#include<stdio.h>

int main()
{
int age = 2;
int num = 0;
for (age = 2; age < 73; age++)
{
if ((age % 10 * 10 + age / 10)==age+27)
{
num++;
//printf("age=%2d,age+27=%2d\n", age, age + 27);
}
}
printf("%d\n", num);
return 0;
}

2.生日蜡烛

int main()
{
int age = 1;
int count = 0;
for (age = 1;age<236; age++)
{
int num = 236;
count = age;
int n = 0;
while (num > n)
{
n += count;
count++;
}
if (n == num)
{
printf("%d\n", age);
}
}
return 0;
}

3.方格填数

+--+--+--+
|  |  |  |
+--+--+--+--+
|  |  |  |  |
+--+--+--+--+
|  |  |  |
+--+--+--+

（如果显示有问题，也可以参看【图1.jpg】）

（左右、上下、对角都算相邻）

#include<stdio.h>
#include<math.h>

int ans = 0;
int my_arr[10] = { 0 };
int arr[10] = { 0 };
int visit[3][4] = { 0 };
int count = 0;
int dir[8][2] = { 0, -1, 0, 1, 1, 0, -1, 0, 1, 1, 1, -1, -1, -1, -1, 1 };
//表示周围的位置

int judge(const int x,const int y)
{
int i = 0;
int tx = 0;
int ty = 0;
for (i = 0; i < 8; i++)
{
tx = x + dir[i][0];
ty = y + dir[i][1];
if ((tx<0) || (tx>2) || (ty<0) || (ty>3))
{
continue;
}
if (visit[tx][ty] == -10)
{
continue;
}
if (abs(visit[tx][ty] - visit[x][y] == 1))
{
return 0;
}
}
return 1;
}

void slove()
{
int i = 0;
int j = 0;
int n = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
if ((i == 0 && j == 0) || (i == 2 && j == 3))
{
visit[i][j] = -10;
}
else
{
visit[i][j] = arr[n];
n++;
}
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
if ((i == 0 && j == 0) || (i == 2 && j == 3))
{
continue;
}
else
{
if (!judge(i,j))
{
return;
}
}
}
}
ans++;
}

void dfs(int index)
{
if (index == 10)
{
slove();
return;
}
else
{
int i = 0;
for (i = 0; i <= 9; i++)
{
if (!my_arr[i])
{
my_arr[i] = 1;
arr[count] = i;
count++;
dfs(index + 1);
arr[count] = 0;
count--;
my_arr[i] = 0;
}
}
}
}

int main()
{
dfs(0);
printf("%d", ans);
return 0;
}

4.快速排序

#include <stdio.h>

void swap(int a[], int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}

int partition(int a[], int p, int r)
{
int i = p;
int j = r + 1;
int x = a[p];
while (1){
while (i<r && a[++i]<x);
while (a[--j]>x);
if (i >= j) break;
swap(a, i, j);
}
//______________________;
swap(a, p, j);
return j;
}

void quicksort(int a[], int p, int r)
{
if (p<r){
int q = partition(a, p, r);
quicksort(a, p, q - 1);
quicksort(a, q + 1, r);
}
}

int main()
{
int i;
int a[] = { 5, 13, 6, 24, 2, 8, 19, 27, 6, 12, 1, 17 };
int N = 12;

quicksort(a, 0, N - 1);

for (i = 0; i<N; i++) printf("%d ", a[i]);
printf("\n");

return 0;
}

5.消除尾一

00000000000000000000000001100111   00000000000000000000000001100000
00000000000000000000000000001100   00000000000000000000000000001100

#include <stdio.h>

void f(int x)
{
int i;
for (i = 0; i<32; i++) printf("%d", (x >> (31 - i)) & 1);
printf("   ");

//x = _______________________;
x = x&(x+1);
for (i = 0; i<32; i++) printf("%d", (x >> (31 - i)) & 1);
printf("\n");
}

int main()
{
f(103);
f(12);
return 0;
}

6.寒假作业

□ + □ = □
□ - □ = □
□ × □ = □
□ ÷ □ = □

(如果显示不出来，可以参见【图1.jpg】)

6  + 7 = 13
9  - 8 = 1
3  * 4 = 12
10 / 2 = 5

7  + 6 = 13
9  - 8 = 1
3  * 4 = 12
10 / 2 = 5

#include<stdio.h>

void bianli(int* a)
{
int i = 0;
for (i = 0; i <= 13; i++)
{
a[i] = 1;
}
}
int main()
{
int sub = 0;
int mul= 0;
int div = 0;
int count = 0;
int i = 0;
int j = 0;
int m = 0;
int n = 0;
{
int a[14] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (i = 1; i <= 13-add; i++)
{
bianli(a);
if ((a[i] != 1) || (a[add+i]!=1))
{
continue;
}
for (sub = 13; sub >0; sub--)
{
bianli(a);
a[i] = 0;
if (a[sub]!= 1)
{
continue;
}
for (j = 1; j <sub; j++)
{
bianli(a);
a[i] = 0;
a[sub] = 0;
if (a[j] != 1)
{
continue;
}
a[j] = 0;
if (a[sub - j] != 1)
{
continue;
}
for (mul = 1; mul <= 13; mul++)
{
bianli(a);
a[i] = 0;
a[sub] = 0;
a[j] = 0;
a[sub - j] = 0;
if (a[mul] != 1)
{
continue;
}
for (m = 1; m <= (13/mul); m++)
{
bianli(a);
a[i] = 0;
a[sub] = 0;
a[j] = 0;
a[sub - j] = 0;
a[mul] = 0;
if (a[m] != 1)
{
continue;
}
a[m] = 0;
if  (a[m*mul] != 1)
{
continue;
}
for (div = 13; div > 0; div--)
{
bianli(a);
a[i] = 0;
a[sub] = 0;
a[j] = 0;
a[sub - j] = 0;
a[m] = 0;
a[mul] = 0;
a[m*mul] = 0;
if (a[div] != 1)
{
continue;
}
a[div] = 0;
for (n = 1; n < div; n++)
{
bianli(a);
a[i] = 0;
a[sub] = 0;
a[j] = 0;
a[sub - j] = 0;
a[m] = 0;
a[mul] = 0;
a[m*mul] = 0;
a[div] = 0;
if ((div%n )!= 0)
{
continue;
}
if (a[n] != 1)
{
continue;
}
a[n] = 0;
if (a[div/n] != 1)
{
continue;
}
count++;
//printf("%d-%d=%d\n", sub, j, sub-j);
//printf("%d*%d=%d\n", mul, m, mul*m);
//printf("%d/%d=%d\n", div, n, div/n);
//printf("\n");
}
}
}
}
}
}
}
}
printf("%d\n", count);
return 0;
} 

7.剪邮票

（仅仅连接一个角不算相连）

#include<stdio.h>
#include<string.h>

int mip[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
int count = 0;
int have[13] = { 0 };//记录五个邮票的位置
int visit[3][4] = { 0 };//判断该邮票是否已经符合
int dir[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 };
//当前邮票的上下左右位置

void find(const int x, const int y)
{
int i = 0;
int tx, ty;
for (i = 0; i<4; i++)
{
tx = x + dir[i][0];
ty = y + dir[i][1];
if ((tx<0) || (tx >= 3) || (ty<0) || (ty >= 4))
{
continue;
}
if ((have[mip[tx][ty]] == 0)|| visit[tx][ty])
//如果该位置没有邮票或者已经判断过了，就跳出
{
continue;
}
visit[tx][ty] = 1;
count++;
find(tx, ty);
}
}

int slove(const int i, const int j, const int k, const int m, const int n)
{
count = 0;
memset(have, 0, sizeof(have));
int a[5] = { i, j, k, m, n };
memset(visit, 0, sizeof(visit));
int num = 0;
for (num = 0; num<5; num++)
{
have[a[num]] = 1;
}
int x, y;
x = i / 4;
y = i % 4-1;
if (i % 4 == 0)
{
x -= 1;
y = 3;
}
count = 1;
visit[x][y] = 1;
find(x, y);
if (count == 5)
{
return 1;
}
return 0;
}

int main()
{
int i = 0;
int j = 0;
int k = 0;
int m = 0;
int n = 0;
int ans = 0;
for (i = 1; i<9; i++)
{
for (j = i + 1; j<10; j++)
{
for (k = j + 1; k<11; k++)
{
for (m = k + 1; m<12; m++)
{
for (n = m + 1; n<13; n++)
{
if (slove(i, j, k, m, n))
{
ans++;
//printf("%d %d %d %d %d\n", i, j, k, m, n);
}
}
}
}
}
}
printf("%d", ans);
return 0;
}

8.四平方和

5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
（^符号表示乘方的意思）

0 <= a <= b <= c <= d

5

0 0 1 2

12

0 2 2 2

773535

1 1 267 838

CPU消耗  < 3000ms

#include<stdio.h>
#include<math.h>

int main()
{
int num = 0;
int count = 0;
int i = 0;
scanf("%d", &num);
int i1 = 0, i2 = 0, i3 = 0;
int x1 = sqrt(num / 4) + 1;
int x2 = sqrt(num / 3) + 1;
int x3 = sqrt(num / 2) + 1;
for (i1 = 0; i1 <= x1; i1++)
{
for (i2 = i1; i2 <= x2; i2++)
{
for (i3 = i2; i3 <= x3; i3++)
{
int x4 = num - i1*i1 - i2*i2 - i3*i3;
if (x4 == (int)sqrt(x4)*(int)sqrt(x4) && (int)sqrt(x4)>=x3)
{
printf("%d %d %d %d\n", i1, i2, i3, (int)sqrt(x4));
exit();
}
}
}
}
return 0;
} 

9.密码脱落(放弃)

X星球的考古学家发现了一批古代留下来的密码。

ABCBA

0

ABDCDCBABC

3

CPU消耗  < 1000ms

10.最大比例(放弃)

X星球的某个大奖赛设了M级奖励。每个级别的奖金是一个正整数。

16,24,36,54

3
1250 200 32

25/4

4
3125 32 32 200

5/2

3
549755813888 524288 2

4/1

CPU消耗  < 3000ms

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客