当前位置: 首页 > news >正文

Removing Digits(Dynamic Programming)

题目描述

You are given an integer n. On each step, you may subtract one of the digits from the number.
How many steps are required to make the number equal to 0?

输入

The only input line has an integer n.
Constraints
1 ≤ n ≤ 10^6

输出

Print one integer: the minimum number of steps.

样例输入
27
样例输出
5
提示

An optimal solution is 27 → 20 → 18 → 10 → 9 → 0.

思路分析

0→0,dp[0]=0;

1→0,dp[1]=1;

2→0,dp[2]=1;

3→0,dp[3]=1;

……

9→0,dp[9]=1;

10→9→0,dp[10]=dp[9]+1=2;

11→10→9→0,dp[11]=dp[10]+1=3;

12→10→9→0,dp[12]=dp[10]+1=3;

或12→11→10→9→0,dp[12]=dp[11]+1=4;(舍)

13→10→9→0,dp[13]=dp[10]+1=3;

或13→12→10→9→0,dp[13]=dp[12]+1=4;(舍)

……

1.初始化dp数组,大小为n+1,dp[0]=0,1~n初始化为1,000,000,000。

2.循环处理,i从1到n,i的每一位数字d,更新dp[i]=min(dp[i],dp[i-d]+1)。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll N=1e9;
ll n;
int main(){cin>>n;vector<ll>dp(n+1,N);dp[0]=0;for(ll i=1;i<=n;i++){ll temp=i;while(temp){int d=temp%10;dp[i]=min(dp[i],dp[i-d]+1);temp/=10;}}cout<<dp[n];return 0;
}
http://www.dtcms.com/a/311238.html

相关文章:

  • SEA-RAFT:更简单、更高效、更准确的RAFT架构
  • 人工智能与交通:智能出行的变革与未来
  • OneCode 3.0表达式从语法到执行的全链路设计
  • 解锁智能油脂润滑系统:加速度与温振传感器选型协同攻略
  • 【隧道篇 / IPsec】(7.6) ❀ 02. 如何删除向导创建的IPsec安全隧道 (点对点) ❀ FortiGate 防火墙
  • 阿里云:Ubuntu系统部署宝塔
  • 【Go语言-Day 29】从time.Now()到Ticker:Go语言time包实战指南
  • eSIM技术深度解析:从物理芯片到数字革命
  • SAP 标准代码测试OO ALV案例分享
  • ubuntu22.04离线一键安装gpu版docker
  • Unity —— Android 应用构建与发布​
  • 社群团购市场选择与开源技术赋能下的下沉市场开拓策略研究——以开源AI智能名片、链动2+1模式与S2B2C商城小程序为例
  • 苹果MAC 安卓模拟器
  • 2561. 重排水果
  • 48Days-Day12 | 添加字符,数组变换,装箱问题
  • 2025牛客暑期多校训练营1(G,E,L,K,I)
  • 力扣 hot100 Day63
  • (LeetCode 面试经典 150 题) 138. 随机链表的复制 (哈希表)
  • Jupyter notebook如何显示行号?
  • 邮科工业交换机:互联网世界的“隐形守护者”
  • 【DL学习笔记】计算图与自动求导
  • K8S部署ELK(一):部署Filebeat日志收集器
  • 红黑树(RBTree)
  • Redis面试精讲 Day 7:GEO地理位置应用详解
  • Mysql在页内是怎么查找数据的?
  • 第14届蓝桥杯Python青少组中/高级组选拔赛(STEMA)2022年11月真题
  • web练习
  • 初始C语言---第四讲(数组)
  • Tlias案例-登录 退出 打包部署
  • 【自动化运维神器Ansible】YAML语法详解:Ansible Playbook的基石