UVA11636 Hello World!

链接

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2683

题解

t = log 2 n ,答案就是 t + [ 2 t < n ]

代码

#include <cstdio>
#include <cctype>
#include <cmath>
int read(int x=0)
{
    char c, f=1;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-48;
    return x*f;
}
int main()
{
    int n, c, t;
    for(n=read(),c=1;n>0;n=read(),c++)
    {
        printf("Case %d: ",c);
        t=log2(n);
        if(1<<t<n)printf("%d\n",t+1);
        else printf("%d\n",t);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/fsahfgsadhsakndas/article/details/81122916