第六届蓝桥杯2015-省赛-C语言大学B组 个人题解

时间:2022-09-10 14:55:41

题目连接:http://course.baidu.com/view/2d86a6c1960590c69fc37622.html

1,奖券数目52488

#include <iostream>
using namespace std;
bool isOK(int a)
{
while(a)
{
if(a%10==4) return false;
a /= 10;
}
return true;
}
int main()
{
int ans = 0;
for(int i=10000; i<=99999; i++)
{
if(isOK(i))
{
ans++;
}
}
cout << ans << endl;
return 0;
}

2,星系炸弹 2017-08-05

#include <iostream>
using namespace std;
bool jwd(int y, int m, int d)
{
if(m==4 || m==6 || m==9 || m==11)
{
return d > 30;
}else if(m==2)
{
if((y%4==0 && y%100!=0) || (y%400==0))
{
return d > 29;
}else{
return d > 28;
}
}else{
return d > 31;
}
}
int main()
{
int y, m, d, ct = 1000;
y = 2014;
m = 11;
d = 9;
while(ct--)
{
d++;
if(jwd(y, m, d))
{
d = 1;
m++;
}
if(m>12)
{
m = 1;
y++;
}
}
cout << y << "-" << m << "-" << d << endl;
return 0;
}

3,三羊献瑞 1085

暴力枚举做的

4,格子中输出(width-2-strlen(buf))/2,"",buf,width-2-(width-2-strlen(buf))/2-strlen(buf),""

5,九数组分数{t=x[k]; x[k]=x[i]; x[i]=t;}

6,加法变乘法16

#include <iostream>
using namespace std;
int main()
{
int a, b, sum = 1225;
for(a=1; a<=49; a++)
{
for(b=a+2; b<=49; b++)
{
int k = sum - 2*a - 1 - 2*b - 1 + a*a + a + b*b + b;
if(k == 2015)
{
cout << a << endl;
}
}
}
return 0;
}
7,牌型种数3598180

#include <iostream>
using namespace std;
int dfs(int pai, int ct)
{
if(pai == 13)
{
return ct <= 4;
}
int res = 0;
res += dfs(pai+1, ct);
if(ct >= 1)
res += dfs(pai+1, ct-1);
if(ct >= 2)
res += dfs(pai+1, ct-2);
if(ct >= 3)
res += dfs(pai+1, ct-3);
if(ct >= 4)
res += dfs(pai+1, ct-4);
return res;
}
int main()
{
cout << dfs(1, 13) << endl;
return 0;
}

8,移动距离

#include <iostream>
using namespace std;
int main()
{
int w, n, m;
int x1, y1, x2, y2;
cin >> w >> m >> n;
x1 = (m + w - 1) / w;
if(x1%2==1)
{
y1 = m - (x1 - 1) * w;

}else{
y1 = w - (m - (x1 - 1) * w) + 1;
}
x2 = (n + w - 1) / w;
if(x2%2==1)
{
y2 = n - (x2 - 1) * w;

}else{
y2 = w - (n - (x2 - 1) * w) + 1;
}
int ans = 0;
if(x1 > x2)
{
ans += x1 - x2;
}else{
ans += x2 - x1;
}
if(y1 > y2)
{
ans += y1 - y2;
}else{
ans += y2 - y1;
}
cout << ans << endl;
return 0;
}
9,垒骰子
#include <iostream>#include <cstring>using namespace std;const int MOD = 1000000007;bool ok[7][7];__int64 dp[110000][7];__int64 dfs(int n,int pre){	if(n==0)	{		return dp[n][pre] = 1;	}	if(dp[n][pre]!=-1) return dp[n][pre];	__int64 res = 0;	for(int i=1; i<=6; i++)	{		if(ok[pre][i])		res += dfs(n-1, i)*4;	}	return dp[n][pre] = res%MOD;}int main(){	int n, m, a, b;	cin >> n >> m;	memset(dp, -1, sizeof (dp));	for(int i=0; i<=6; i++)	{		for(int j=0; j<=6; j++)		{			ok[i][j] = true;		}	}	while(m--)	{		cin >> a >> b;		ok[a][b] = ok[b][a] = false;	}	cout << dfs(n, 0) << endl;	return 0;}

这个只能过60%,数学不会没办法,看大神的题解好像是要矩阵快速乘法

10,生命之树
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
using namespace std;
vector<int>G[100010];
int val[100010];
bool use[100010];
__int64 ans;
__int64 dfs(int xb)
{
use[xb] = true;
__int64 res = val[xb];
for(int i=0; i<G[xb].size(); i++)
{
int k = G[xb][i];
if(!use[k])
{
int b = dfs(k);
if(b>0)
res += b;
}
}
if(ans < res)
{
ans = res;
}
return res;
}
int main()
{
int n, i, a, b;
scanf("%d", &n);
for(i=1; i<=n; i++)
{
scanf("%d", &val[i]);
G[i].clear();
}
for(i=1; i<n; i++)
{
scanf("%d %d",&a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
memset(use, 0, sizeof (use));
ans = val[1];
dfs(1);
cout << ans << endl;
return 0;
}

蓝桥杯已经算是告一段落咯,数学题目什么都还不会,得要加油学习了!