蓝桥杯每日一题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;
}
相关推荐
CoderYanger3 小时前
视频切割脚本(Python版)
linux·开发语言·windows·后端·python·程序人生·职场和发展
CoderYanger3 小时前
视频裁剪+缩放+自动添加水印脚本(Python版)
开发语言·后端·python·程序人生·职场和发展·音视频·学习方法
杨逢昌工厂6S管理1 天前
28- 杨逢昌:制造企业仓储6S反复混乱解决方案——四维定置标准化管控体系,让物料混放、通道堵塞等重复性乱象减少90%
大数据·经验分享·笔记·职场和发展·制造·学习方法
想吃火锅10051 天前
【leetcode】56.合并区间js
算法·leetcode·职场和发展
凌波粒1 天前
LeetCode--47.全排列 II(回溯算法)
算法·leetcode·职场和发展
天真小巫1 天前
2026.7.10总结
职场和发展
程序员小远1 天前
性能测试之性能调优
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·性能测试
aqiu1111111 天前
【算法日记 19】LeetCode 1. 两数之和:梦开始的地方,哈希表的降维打击
算法·leetcode·职场和发展
兰令水2 天前
leecodecode【面试150】【2026.7.9打卡-java版本】
java·数据结构·leetcode·面试·职场和发展
程序员小远2 天前
接口测试之文件上传
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试