ZOJ 3498 Javabeans

脑筋急转弯。

如果是偶数个,那么第一步可以是$n/2+1$位置开始到$n$都减去$n/2$,后半段就和前半段一样了。

如果是奇数个,那么第一步可以是$(n+1)/2$位置开始到$n$都减去$(n+1)/2$,$(n+1)/2$位置变成了$0$,之后的就和前半段一样了。

递归下去即可。因此,答案就是$ans(n/2)+1$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-10;
void File()
{
    freopen("D:\in.txt","r",stdin);
    freopen("D:\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar();
    x = 0;
    while(!isdigit(c)) c = getchar();
    while(isdigit(c))
    {
        x = x * 10 + c - '0';
        c = getchar();
    }
}

int T;
int n;

int get(int x)
{
    if(x==1) return 1;
    return get(x/2)+1;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        printf("%d
",get(n));
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/6384496.html