栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2021蓝桥杯省赛b题解(无题目)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021蓝桥杯省赛b题解(无题目)

  1. 卡片

#include

using namespace std;

const int maxn = 2021;

int  car[10];

int get(int x) {

return x % 10;

}

int main(){

for (int i = 0; i < 10; i++) {

car[i] = maxn;

}

car[0]--;

for (int k = 1;; k++) {

int i = k;

while (i) {

int x=get(i);

car[x]--;

if (car[x] == -1) {

cout << k-1 << endl;//因为这个数字已经不能拼了

}

i /= 10;

}

}

return 0;

}

  1. 直线

#include

#include

using namespace std;

set>line;

double ins = 1e-5;

int main() {

double  x1, y1, x2, y2;

for (x1 = 0; x1 < 20; x1++) {

for (y1 = 0; y1 < 21; y1++) {

for (x2 = 0; x2 < 20; x2++) {

for (y2 = 0; y2 < 21; y2++) {

if (x1 != x2 && y1 != y2) {

double k = (y2 - y1) / (x2 - x1);

//double b = y2 - k * x2;如果按照第一种写法,

//那么y2是int型的,int减double会精度缺失,

//像第二种解法,同分了就比较高精度。

double b = (y2 * (x2 - x1) - (y2 - y1) * x2) / (x2 - x1);

pair newline;

newline.first = k;

newline.second = b;

line.insert(newline);

}

}

}

}

}

printf("%d", line.size() + 20 + 21);

return 0;

}

  1. 货物摆放

求这个数的约数用约数排列组合

因为只有是约数才有可能乘机是这个数

#include

using namespace std;

typedef long long LL;

LL n = 2021041820210418;

LL mun[101000], cnt=0;

int main() {

for (LL i = 1; i <= n/i; i++) {

if (n % i == 0) {

mun[++cnt] = i;

if (i * i != n) {

mun[++cnt] = n / i;

}

}

}

cout << cnt << endl;

int ans = 0;

for (int i = 1; i <= cnt; i++) {

for (int j = 1; j <= cnt; j++) {

if (n % (mun[i] * mun[j]) == 0) {

ans++;

}

}

}

cout << ans << endl;

return 0;

}

  1. 路径

#include

using namespace std;

int mp[2022][2022];

const int inf = 0x3f3f3f3f;

int d[2022];//大一点

bool vis[2022];

int gcd(int a, int b) {

return b ? gcd(b, a % b) : a;

}

int main() {

for (int i = 1; i <= 2021; i++) {

for (int j = 1; j <= 2021; j++) {

if (i == j) {

mp[i][j] = 0;

}

else if (abs(i - j) > 21) {

mp[i][j] = mp[j][i] = inf;

}

else {

mp[i][j] = mp[j][i] = i * j / gcd(i, j);

}

}

}

//floyd

//dijkstra

memset(vis, false, sizeof(vis));

memset(d, 0x3f3f3f3f, sizeof(d));

d[1] = 0;

for (int i = 1; i <= 2021; i++) {

int x = 0;

for (int j = 1; j <= 2021; j++) {

if (!vis[j]&&d[j]

x = j;

}

}

vis[x] = true;

for (int j = max(1, x - 21); j <= min(2021, x + 21); j++) {//剪枝只有这些边有值才会有边

d[j] = min(d[j], d[x] + mp[x][j]);

}

}

//cout << mp[1][2021] << endl;

cout << d[2021] << endl;

return 0;

}

  1. 时间显示

#include

using namespace std;

int main(){

long long time,s;

int h, m;

cin >> time;

s = time / 1000;

h = s / 3600 % 24;

s = s % 3600;

m = s / 60 ;

s = s % 60;

printf("%02d:%02d:%02ld", h, m, s);

return 0;

}

  1. 砝码称重

#include

using namespace std;

const int N = 110,M=100010;

int a[N],m;

bool dp[N][M];

int main() {

int n;

cin >> n;

for (int i = 1; i <= n; i++) {

cin >> a[i];

m += a[i];

}

dp[0][0] = true;

for (int i = 1; i <= n; i++) {

for (int j = 0; j <= m; j++) {

dp[i][j] = dp[i - 1][j] || dp[i - 1][j + a[i]] || dp[i - 1][abs(j - a[i])];

}

}//这是分成三种情况第一个是没放的

//第二个放在右面

//第三个放在左边所以要减去

//每个都三种情况

//然后一次加入每个砝码就就可以得到结果类似广度优先搜索

int ans = 0;

for (int i = 1; i <= m; i++) {

if (dp[n][i]) {

ans++;

}

}

cout << ans << endl;

return 0;

}

  1. 杨辉三角形

#include

using namespace std;

int a[2005][2005];

int main() {

//二项式问题

//二项式定理,对于C(3,n),当n等于2000时,C(3,2000)>1e9

//因此只需要算到第2000行就好了,剩下的再算C(1,n)和C(2,n)就好了。

int n;

cin >> n;

memset(a, 0, sizeof(a));

a[0][0]=1;

for (int i = 1; i <= 2005; i++) {

for (int j = 1; j <= i; j++) {

a[i][j] = a[i - 1][j] + a[i - 1][j - 1];

if (a[i][j] == n) {

cout << i * (i - 1) / 2 + j << endl;

return 0;

}

}

}

int a;

a = sqrt(n * 2) + 1;//杨辉三角和二项式的关系

if (a * (a - 1) / 2 == n) {//c(2 ,n)c是排列a是组合

cout << a * (a - 1) / 2 + 3 << endl;

}

else {

cout << n * (n - 1) / 2 + 2 << endl;

}

return 0;

}

  1. 双向排序(部分分值)

#include

#include

using namespace std;

int a[100010];

bool cmp(int a, int b) {

return a > b;

}

int main() {

int n, m;

cin >> n >> m;

for (int i = 1; i <= n; i++)a[i] = i;

while (m--) {

int p, q;

cin >> p >> q;

if (p == 0) {

//sort(a + 1, a + q + 1, greater());

sort(a + 1, a + q + 1, cmp);

}else{

sort(a + q, a + 1 + n);

}

}

for (int i = 1; i <= n; i++) {

cout << a[i] << " ";

}

cout << endl;

return 0;

}

  1. 括号序列

错误

思路在右括号多时从右边开始反过来

#include

#include

using namespace std;

int ans=0;

int x = 0;//第几个符号

int lm=0, rm=0,mun,vis;

string s;

void bfs(int x,int l,int r) {

if (x == s.size()) {

if (l == r)ans++;

cout << l << " " << r<

return;

}

char c=s[x];

if (c == '(') {

l++;

}

else {

r++;

}

cout <<" "<< l << " " << r << endl;

for (int i = abs(l - r); i >= 0; i--) {

if (vis) {

if (i)vis -= i;

if (mun) {

bfs(x + 1, r + i, l);

}

else {

bfs(x + 1, l, r + i);

}

if (i)vis += i;

}

else  if (i == 0) {

bfs(x + 1, l, r);

}

}

if (c == '(') {

l--;

}

else {

r--;

}

return;

}

int main() {

cin >> s;

for (int i = 0; i < s.size(); i++) {

if (s[i] == '(')lm++;

if (s[i] == ')')rm++;

}

mun = rm - lm;

vis = abs(rm - lm);

cout << mun << endl;

bfs(0,0,0);

cout << ans<

return 0;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/766904.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号