蓝桥杯每日一题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;
}
相关推荐
Nil2084 小时前
leetcode 105从前序和中序遍历序列构造二叉树
算法·leetcode·职场和发展
测试19987 小时前
接口自动化测试的全面解析与实战指南
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
FPC工厂——皇榜科技8 小时前
【皇榜科技线路板质量课堂·第40篇】PPAP(生产件批准程序):批量生产前的“终极面试”
科技·面试·职场和发展·制造·pcb工艺
Forever Nore10 小时前
LeetCode 16 最接近的三数之和 - 双指针逼近
算法·leetcode·职场和发展
重生之后端学习11 小时前
438. 找到字符串中所有字母异位词[中等]✅
开发语言·数据结构·算法·leetcode·职场和发展
土司大王1 天前
LeetCode hot100——两两交换链表中的节点
算法·leetcode·职场和发展
测试19981 天前
Selenium 无法定位元素的几种解决方案
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
AIHR数智引擎1 天前
如何用WorkBuddy跑通HR自动化流程?
人工智能·经验分享·chatgpt·职场和发展·自动化·ai-native