蓝桥杯每日一题2023.10.16

数的分解 - 蓝桥云课 (lanqiao.cn)

题目描述

题目分析

最开始想使用dfs,发现范围过大无法在规定时间运行

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], v[N], ans;
void dfs(int dep, int sum, int start)
{
	if(sum > 2019)return;
	if(dep > 3)return;
	if(dep == 3)
	{
		int flag = 0;
		for(int i = 0; i < 3; i ++)
		{
			int x = a[i];
			//cout << x << ' ';
			while(x)
			{
				int y = x % 10;
				x /= 10;
				if(y == 2 || y == 4)flag = 1;
			}
		}
		if(!flag && sum == 2019)ans ++;
		//cout << '\n';
		return;
	}
	for(int i = start; i <= 2017; i ++)
	{
		a[dep] = i;
		dfs(dep + 1, sum + i, i + 1);
		a[dep] = 0;
	}
}
int main()
{
	dfs(0, 0, 1);
	cout << ans;
	return 0;
}

故直接简单枚举即可

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int ans;
bool check(int n)
{
	while(n)
	{
		int y = n % 10;
		if(y == 2 || y == 4)return false;
		n /= 10;
	}
	return true;
}
int main()
{
	for(int i = 1; i <= 2019; i ++)
	{
		for(int j = i + 1; j <= 2019; j ++)
		{
			int k = 2019 - i - j;
			if(check(i) && check(j) && check(k))
			{
				if(j < k)ans ++;
			}
		}
	}
	cout << ans;
}
相关推荐
qq_4592344211 天前
【题库】| 商用密码应用安全性评估从业人员考核题库(四十)
职场和发展·密码学·学习方法·考核·商用密码·商用密码应用安全性评估·密评
敲敲了个代码11 天前
[特殊字符] 空数组的迷惑行为:为什么 every 为真,some 为假?
前端·javascript·react.js·面试·职场和发展
诚思报告YH11 天前
视频面试软件市场洞察:2026 - 2032年复合年均增长率(CAGR)为10.3%
面试·职场和发展
重生之后端学习11 天前
74. 搜索二维矩阵
开发语言·数据结构·算法·职场和发展·深度优先
tyb33333311 天前
leetcode:吃苹果和队列
算法·leetcode·职场和发展
Pitiless-invader11 天前
MySQL 相关知识及面试问题汇总
面试·职场和发展
重生之后端学习11 天前
35. 搜索插入位置
java·数据结构·算法·leetcode·职场和发展·深度优先
逆境不可逃11 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式
筱昕~呀11 天前
冲刺蓝桥杯-DFS板块(第二天)
算法·蓝桥杯·深度优先
zheshiyangyang11 天前
前端面试基础知识整理【Day-10】
前端·面试·职场和发展