当前位置:网站首页>To 3 --- 最后的编程挑战
To 3 --- 最后的编程挑战
2022-06-29 09:23:00 【陌陌623】
算是比较典型的dfs
每以为两种情况 这个不难
边界条件比较不容易判断,做的时候,调试了不少次才可
sum是全部位上的和,只有sum%3==0就可
删除:sum剪掉这一位上的数字
不删除:记得i要移动,意义是判断第i位
至于用string存储,感觉这个还是很容易想出的。
dfs中我感觉写的并不是太好,大佬看见的话,可以评论发下代码哦~。
#include <iostream>
using namespace std;
using ll = long long;
string N;
ll dfs(int k, int sum, int i)
{
ll res = 1e18;
// 0:全部都删去的情况
if (sum == 0)
return res;
if (sum % 3 == 0)
return k;
// 将第一个数字删除 i也是-1 所以sum%3的判断在上面
if (i == -1)
return res;
// 删除与否第i的数的两种情况
res = min(res, dfs(k, sum, i - 1));
res = min(res, dfs(k + 1, sum - N[i] + '0', i - 1));
return res;
}
int main()
{
cin >> N;
int sum = 0;
for (auto e : N)
sum += e - '0';
ll res = dfs(0, sum, N.size() - 1);
cout << (res == 1e18 ? -1 : res) << endl;
return 0;
}
边栏推荐
猜你喜欢

520 钻石争霸赛 2021

Codeforces Round #645 (Div. 2)

HDU 6778 Car (分组枚举-->状压 dp)

JVM method return address

完美二叉树、完全二叉树、完满二叉树

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Application of keil5 integrated development environment for single chip microcomputer

Listview of the basic component of the shutter

JVM之TLAB

The collapsing "2.3 * 10 = 22" produced by multiplying float and int
随机推荐
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
Nacos环境隔离
PHP内存马技术研究与查杀方法总结
图片验证码控件
Application of decorator mode, packaging ServletRequest and adding addparameter method
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
十六制计数器和流水灯
自定义控件之侧滑关闭 Activity 控件
Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
Flutter 基础组件之 Image
2019.10.23训练总结
Language characteristics
1099 Build A Binary Search Tree (30 分)
FreeRTOS (IX) - queue
容器
KDevelop new project
2019.10.27训练总结
Using rancher to build kubernetes cluster
2019-11-10训练总结
基辅周边的凄美废墟——切尔诺贝利的安全前往指南!