zoukankan      html  css  js  c++  java
  • UVa 11636 Hello World!

      很简单的一道题,可以直接模拟,需要注意的是以一个负数结束,不要被样例的-1误导了。

      代码如下:

    View Code
     1 #include <cstdio>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int n, kase = 0;
     7     while(scanf("%d", &n) != EOF)
     8     {
     9         if(n < 0)   break;
    10         kase++;
    11         int num = 1, cnt = 0;
    12         while(num < n)
    13         {
    14             num *= 2;
    15             cnt++;
    16         }
    17         printf("Case %d: %d\n", kase, cnt);
    18     }
    19     return 0;
    20 }

      下面是用log函数算的,运行时间和上面代码时间一样,都是0.008s,应该是测试数据比较小,数据量也不多的缘故吧...

      代码如下:

      

    View Code
     1 #include <cstdio>
     2 #include <cmath>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int n, kase = 0;
     8     while(scanf("%d", &n) && n >= 0)
     9     {
    10         int ans;
    11         if(!n)   ans = 0;
    12         else ans = (int)ceil(log(n)/log(2));
    13         printf("Case %d: %d\n", ++kase, ans);
    14     }
    15     return 0;
    16 }
    17         
  • 相关阅读:
    VS2013快速安装教程
    软件工程课程的感想
    GitHub和Microsoft TFS对比有什么优势
    拼接素数
    C语言程序题
    vue中的实例方法的底层原理
    ios 安卓
    防抖
    伪数组转真数组的放法
    http和https的一种能力?
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/2999705.html
Copyright © 2011-2022 走看看