您的位置:首页 > 其它

UVA11384—Help is needed for Dexter

2013-11-09 22:24 295 查看
Dexter is tired of Dee Dee. So he decided to keep Dee Dee busy in a game. The game he planned for her is quite easy to play but not easy to win at least not for Dee Dee. But Dexter does not have time to spend on this silly task, so he wants your help.



There will be a button, when it will be pushed a random number N will be chosen by computer. Then on screen there will be numbers from 1 to N. Dee Dee can choose any number of numbers from the numbers on the screen, and then she will command computer to
subtract a positive number chosen by her (not necessarily on screen) from the selected numbers. Her objective will be to make all the numbers 0.



For example if N = 3, then on screen there will be 3 numbers on screen: 1, 2, 3. Say she now selects 1 and 2. Commands to subtract 1, then the numbers on the screen will be: 0, 1, 3. Then she selects 1 and 3 and commands to subtract 1. Now the numbers are
0, 0, 2. Now she subtracts 2 from 2 and all the numbers become 0.



Dexter is not so dumb to understand that this can be done very easily, so to make a twist he will give a limit L for each N and surely L will be as minimum as possible so that it is still possible to win within L moves. But Dexter does not have time to think
how to determine L for each N, so he asks you to write a code which will take N as input and give L as output.



Input and Output:

Input consists of several lines each with N such that 1 ≤ N ≤ 1,000,000,000. Input will be terminated by end of file. For each N output L in separate lines.



SAMPLE INPUT

OUTPUT FOR SAMPLE INPUT

1

2

3

1

2

2

分析:

有一个从1、2、3、.......n的正整数数列,现在要通过若干次操作将这个数列全部变为0,操作的方法是每次选取任意1个或多个任意位置的数,然后将这几个数同时减去一个一个数。问至少要经过多少次操作才能让数列全部变为0。





拿到这道题目之后,最好的方式是自己试一试。经过若干次尝试和总结后,不难发现第一步的最好方式如图所示。



换句话说,当n=6的时候留下1, 2, 3,而把4,5,6同时减去min{4,5,6}=4得到序列1, 2, 3, 0, 1, 2,它等价于1, 2, 3(想一想,为什么)。换句话说,f(6)=f(3)+1。

一般地,为了平衡,我们保留1~n/2,把剩下的数同时减去n/2+1,得到序列1,2, …,n/2, 0,1,…,(n-1)/2,它等价于1,2, …,n/2,因此f(n)=f(n/2)+1。边界是f(1)=1。代码如下。



#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
int f(int n)
{
    return n==1?1:f(n/2)+1;
}
int main()
{
   int n,x;
   while(scanf("%d",&n)!=EOF)
   {
//       scanf("%d",&x);
       printf("%d\n",f(n));
   }
    return 0;
}


一道相似题目:


3917:果冻豆分数: 1.7

时间限制:1 秒

内存限制:32 兆

特殊判题: 否

提交:41

解决: 27

题目描述

安卓4.1版本的代号叫果冻豆(Jelly Bean),当时发布的时候,小明就对这个果冻豆很好奇,因为他以前没有吃过果冻豆,于是他去买了一盒果冻豆,当他吃下第一颗果冻豆的时候,他爱上了果冻豆。

之后小明又买了好多果冻豆,小明把它们放到n个盒子里,在第i个盒子里有i个果冻豆(i=1,2,3,...,n)。小明每天选择一个整数x,再选择一些装着果冻豆的盒子,这些被选中的盒子每个盒子里的果冻豆数量至少为x,然后小明吃掉每个被选中的盒子里的x个果冻豆。小明想尽快吃完所有的果冻豆,越快越好,所以想请你计算一下小明最快需要几天可以吃完所有的果冻豆?

输入格式

输入包含多组测试数据。

输入的第一行是一个整数T,表示有T组测试数据。

每组输入一个正整数n,n的含义见题目描述,n在int范围内。

输出

对于每组输入,输出所要求的结果。

样例输入

4

1

2

3

4

样例输出

1

2

2

3

一模一样~



#include<iostream>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<math.h>
#include<vector>
#include<map>
#include<deque>
#include<list>
using namespace std;
int f(int n)
{
    return n==1?1:f(n/2)+1;
}
int main()
{
   int n,x;
   scanf("%d",&n);
   while(n--)
   {
       scanf("%d",&x);
       printf("%d\n",f(x));
   }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: