您的位置:首页 > 其它

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: