您的位置:首页 > 编程语言 > C语言/C++

2016年蓝桥杯C&C++程序设计本科B组

2017-05-20 23:17 357 查看


煤球数目


Description

有一堆煤球,对称三角棱锥形。
第一层 放 1个
第二层 放 3个
第三层 放 6个
第四层 放 10个
...
如果一共有100层,共有多少个煤球?


Analyze

1  ->  1
2  ->  1+2
3  ->  1+2+3
4  ->  1+2+3+4
...
n  ->  (1+n)*n/2


Code

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
int sum = 0, n=0;

while(cin >> n)
{
for(int i=1; i<=n; i++)
sum += (i + 1) * i / 2;
cout << sum << endl;
}

return 0;
}


生日蜡烛


Description

某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛。现在算起来,他一共吹熄了236根蜡烛。
请问,他从多少岁开始过生日party的?请填写他开始过生日party的年龄数。


Analyze

换句话说就是让我们求 一段连续的自然数的和是236,问这段数从多少开始?

设从m岁开始过生日,到n岁一共吹吹了236根蜡烛

(n*(n+1))/2 - (m*(m+1))/2 = 236
==>	(n-m)*(n+m+1) / 2 = 236

这样我们就可以两层 for 循环直接暴力就可以了


Code

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
bool is_find = false;
for(int i=2; i<=100; i++)
{
for(int j=1; j<i; j++)
{
if (((i-j)*(i+j+1))/2 == 236)
{
cout << j+1 << endl;
break;
is_find = true;
}
}
if(is_find)
break;
}

return 0;
}


凑算式


Description

A + B/C + DEF/GHI = 10

这个算式中 A~I 表示 1~9 的数字,不同的字母代表不同的数字。

比如:
6 + 8/3 + 952/714 = 10
5 + 3/1 + 972/486 = 10
求这个算式一共有多少种解法?


Analyze

直接暴力解决,没有想到其他的好办法。


Code

#include <iostream>
using namespace std;
int main()
{
int sum=0;
for(int a=1; a<=9; a++)
for(int b=1; b<=9; b++)
{
if(a==b) continue;
for(int c=1; c<=9; c++)
{
if(c==a||c==b) continue;
for(int d=1; d<=9; d++)
{
if(d==a||d==b||d==c)continue;
for(int e=1; e<=9; e++)
{
if(e==a||e==b||e==c||e==d) continue;
for(int f=1; f<=9; f++)
{
if(f==a||f==b||f==c||f==d||f==e) continue;
for(int g=1; g<=9; g++)
{
if(g==a||g==b||g==c||g==d||g==e||g==f) continue;
for(int h=1; h<=9; h++)
{
if(h==a||h==b||h==c||h==d||h==e||h==f||h==g)
continue;
for(int i=1; i<=9; i++)
{
if(i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h)
continue;
int t1=a*c*(100*g+10*h+i);
int t2=b*(100*g+10*h+i);
int t3=c*(100*d+10*e+f);
int t4=10*c*(100*g+10*h+i);
if(t1+t2+t3==t4)
sum++;
}
}
}
}
}
}
}
}

cout<<sum<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: