UVA11384-Help is needed for Dexter-水题/贪心
2015-10-31 09:54
344 查看
给出n;表示数字1-n的一个序列
每次选一个或多个数减去 相同的正整数, 问最少步骤使得最后全为0;
贪心: 显然每次选择n/2 来 作为要被减去的值能最大限度的降低整个序列的值
其次是, 例如 第一次选了n/2,然后比他大的数,包括本身 都减去n/2
如果n%2==1,那么得到新的两个 [1,n/2] 数列, 其实他们可以看作一个[1,n/2] 了.....所以每次的结果就是缩小n的值
如果为偶数也一样...
最后把n缩减到1. 最后一次全部减一就完成了.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
while(n!=1)
{
ans++;
n=n/2;
}
printf("%d\n",ans+1);
}
return 0;
}
每次选一个或多个数减去 相同的正整数, 问最少步骤使得最后全为0;
贪心: 显然每次选择n/2 来 作为要被减去的值能最大限度的降低整个序列的值
其次是, 例如 第一次选了n/2,然后比他大的数,包括本身 都减去n/2
如果n%2==1,那么得到新的两个 [1,n/2] 数列, 其实他们可以看作一个[1,n/2] 了.....所以每次的结果就是缩小n的值
如果为偶数也一样...
最后把n缩减到1. 最后一次全部减一就完成了.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int ans=0;
while(n!=1)
{
ans++;
n=n/2;
}
printf("%d\n",ans+1);
}
return 0;
}
相关文章推荐
- Django RestFramework源码剖析(1)——设定不同的serializer
- 关于VS2013中To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
- 高精度小数
- 解决Mysql ERROR 1 (HY000): Can't create/write to file 问题
- 超实用的JavaScript代码段 Item1 --倒计时效果
- JavaScript中的delete,typeof,instanceof运算符
- Centos磁盘挂载操作
- 超实用的JavaScript代码段 Item1 --倒计时效果
- CTP报单状态 OrderStatus全部状态
- asp.net在IIS环境中访问第三方需数字证书接口时 报 “请求被中止: 未能创建 SSL/TLS 安全通道”
- 机器学习、模式识别与数据挖掘之间的关系
- jQuery 人脸识别插件,支持图片和视频
- 使用Memory Analyzer Tool(MAT)Eclipse插件分析内存泄露
- JavaScript学习2-BOM
- Spring(1)--helloworld
- 当你输入一个网址的时候,实际会发生什么?
- //debug 在打包上线时所有的NSlog输出都被屏蔽了
- 联想Y410p 重新装cn windows 8 1 x64 dvd 2707237系统步骤及问题解决
- [2012]:素数判定
- 【《zw版·Halcon与delphi系列原创教程》 zw_halcon人脸识别