# 蓝桥杯第七届B.C组C

1.煤球数目

....

#include<stdio.h>

int main()
{
int i = 0;
int num = 0;
int a[101] = { 0 };
for (i = 1; i < 101; i++)
{
a[i] = a[i - 1] + i;
num += a[i];
}
printf("%d", num);

return 0;
}

3.凑算式

B      DEF
A + --- + ------- = 10
C      GHI

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

6+8/3+952/714 就是一种解法，
5+3/1+972/486 是另一种解法。

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

int  arr[10] = { 0 };
int ans = 0;
int visit[10] = { 0 };

int judge(int* arr)
{
double x = arr[1] + (double)arr[2] / arr[3] + (double)(arr[4] * 100 + arr[5] * 10 + arr[6]) / (arr[7] * 100 + arr[8] * 10 + arr[9]);
if (x == 10)
{
return 1;
}
return 0;
}

int main()
{
int a, b, c, d, e, f, g, h, i;
for (a = 1; a <= 9; a++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
for (b = 1; b <= 9; b++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
if (arr[b] == 1)
{
continue;
}
arr[b] = 1;
for (c = 1; c <= 9; c++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
if (arr[c] == 1)
{
continue;
}
arr[c] = 1;
for (d = 1; d <= 9; d++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
if (arr[d] == 1)
{
continue;
}
arr[d] = 1;
for (e = 1; e <= 9; e++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
if (arr[e] == 1)
{
continue;
}
arr[e] = 1;
for (f = 1; f <= 9; f++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
arr[e] = 1;
if (arr[f] == 1)
{
continue;
}
arr[f] = 1;
for (g = 1; g <= 9; g++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
arr[e] = 1;
arr[f] = 1;
if (arr[g] == 1)
{
continue;
}
arr[g] = 1;
for (h = 1; h <= 9; h++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
arr[e] = 1;
arr[f] = 1;
arr[g] = 1;
if (arr[h] == 1)
{
continue;
}
arr[h] = 1;
for (i = 1; i <= 9; i++)
{
memset(arr, 0, sizeof(arr));
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
arr[e] = 1;
arr[f] = 1;
arr[g] = 1;
arr[h] = 1;
if (arr[i] == 1)
{
continue;
}
arr[i] = 1;
visit[1] = a;
visit[2] = b;
visit[3] = c;
visit[4] = d;
visit[5] = e;
visit[6] = f;
visit[7] = g;
visit[8] = h;
visit[9] = i;
if (judge(visit))
{
ans++;
}
}
}
}
}
}
}
}
}
}
printf("%d", ans);
return 0;
}

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

int arr[10] = { 0 };
int ans = 0;
int my_arr[10] = { 0 };

void slove()
{
double x = arr[1] + (double)arr[2] / arr[3] + (double)(arr[4] * 100 + arr[5] * 10 + arr[6]) / (arr[7] * 100 + arr[8] * 10 + arr[9]);
if (x == 10)
{
ans++;
}
}

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

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

5.

X星球要派出一个5人组成的观察团前往W星。

A国最多可以派出4人。
B国最多可以派出2人。
C国最多可以派出2人。
....

DEFFF
CEFFF
CDFFF
CDEFF
CCFFF
CCEFF
CCDFF
CCDEF
BEFFF
BDFFF
BDEFF
BCFFF
BCEFF
BCDFF
BCDEF
....
(以下省略，总共101行)

#include <stdio.h>
#define N 6
#define M 5
#define BUF 1024

void f(int a[], int k, int m, char b[])
{
int i,j;

if(k==N){
b[M] = 0;
if(m==0) printf("%s\n",b);
return;
}

for(i=0; i<=a[k]; i++){
for(j=0; j<i; j++) b[M-m+j] = k+'A';
//______________________;  //填空位置
f(a, k + 1, m-i, b);
}
}
int main()
{
int  a[N] = {4,2,2,1,1,3};
char b[BUF];
f(a,0,M,b);
return 0;
}

9.交换瓶子

2 1 3 5 4

1 2 3 4 5

5
3 1 2 5 4

3

5
5 4 3 2 1

2资源约定：

CPU消耗  < 1000ms

一开始看到这个题，就想的是就是给这几个瓶子排个顺序就好了，但是写完之后发现时间太长了，再读一遍题才发现，瓶子标号是1-N，。。。。。这样用一个数组进行计数，然后一个遍历，for循环每循环一次，当前位置的瓶子就是正确的标号。这里感觉还有一个要注意的点就是交换的时候，如果新建一个变量进行交换的时候，会有一点点的慢，用异或的方式，以二进制的形式进行交换好一点。

#include<stdio.h>

int ans = 0;
int arr[10010] = { 0 };
int N = 0;
int i = 0;

void Exchange(int* a, int* b)
{
*a = *a^*b;
*b = *a^*b;
*a = *a^*b;
ans++;
}

int main()
{
scanf("%d", &N);
for (i = 1; i <= N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i <= N; i++)
{
while (arr[i] != i)
{
Exchange(&arr[i], &arr[arr[i]]);
}
}
printf("%d", ans);
return 0;
}

C
1.报纸页数

X星球日报和我们地球的城市早报是一样的，

1125,1126,1727,1728

#include<stdio.h>

int main()
{
int a[4] = { 1125, 1126, 1727, 1728 };
int page = a[3] + a[0] + 1;
printf("%d", page);
return 0;
}

3.平方怪圈

#include<stdio.h>

int main()
{
int i = 0;
int a[1000] = { 0 };
int b[1000] = { 0 };
int n = 0;
scanf("%d", &n);
while (a[n] == 0 )
{
int sum = 0;
int num = n;
a[n] = 1;
while (num)
{
sum += (num % 10)*(num % 10);
num /= 10;
}
b[i] = sum;
i++;
n = sum;
}
int j = 0;
for (j = 0; j < i; j++)
{
printf("%d ", b[j]);
}
printf("%d", n);
return 0;
}

9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1

10

52

100

9232资源约定：

CPU消耗  < 1000ms

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

long long operation(long long number)
{
if (number % 2 == 0)
{
return number / 2;
}
return number * 3 + 1;
}

int main()
{
long long j = 0;
long long max = 0;
long N=0;
scanf("%ld", &N);
for (j = 1; j <= N; j++)
{
long long num = j;
while (num != 1)
{
num = operation(num);
if (num > max)
{
max = num;
}
}
}
printf("%lld\n", max);
return 0;
}

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