UVa 11384 Help is needed for Dexter

本文介绍了一种算法,用于计算将序列1到n中所有数变为0所需的最少操作次数。通过递归方式实现了该算法,并给出了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给定n,用最少的操作数把序列1到n中的所有数都变成0,每次可以选择一个或者多个数,同时减去一个相同的正整数

发现多画几次,f(n) = f(n/2) + 1,大概就是想要每次变化之后两端都尽可能对称的感觉

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 
18 int f(int n){
19     if(n == 1) return 1;
20     return f(n/2) + 1;
21 }
22 
23 int main(){
24     int n;
25     while(scanf("%d",&n) != EOF){
26         printf("%d\n",f(n));
27     }
28     return 0;
29 }
View Code

 

转载于:https://www.cnblogs.com/wuyuewoniu/p/4622746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值