Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1559240
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2018-03-04 16:59:47





  1. // lc0069.cpp 二分法
  2. // C语言中long和int是一样的,long long才是真正的64bit
  3. // 32bit最大int 是2147483647 或者0x7FFFFFFF

  4. #include "stdafx.h"
  5. #include <stdio.h>

  6. int guess(long long x, long long y){
  7.    if((x*x) <= y)
  8.       return 1;
  9.    else
  10.       return 0;
  11. }

  12. int mysqrt(int y){
  13.    long long L=0,R=(long long)y+1;//[0,y)
  14.    long long ans=0;
  15.    long long mid;
  16.    while(L<R){
  17.       mid=(L+R)/2;
  18.       if(guess(mid,y)){
  19.          ans=mid;
  20.          L=mid+1;
  21.       }else{
  22.          R=mid;
  23.       }
  24.    }
  25.    return (int)ans;
  26. }
  27. int main(int argc, char* argv[])
  28. {
  29.    int T,i,y,result;
  30.    freopen("lc0069i.txt","r",stdin);
  31.    scanf("%d",&T);
  32.    for(i=0;i<T;i++){
  33.       scanf("%d",&y);
  34.       result=mysqrt(y);
  35.       printf("Result is %d\n", result);
  36.    }
  37.    return 0;
  38. }
  39. /*
  40. 5
  41. 361
  42. 10000
  43. 16
  44. 2147395599
  45. 2147483647
  46. */



  1. // lc0069.cpp 这里一定要注意的是
  2. // C语言中long和int是一样的,long long才是真正的64bit
  3. // 32bit最大int 是2147483647 或者0x7FFFFFFF

  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <windows.h>

  8. int guess(long long y, long long x){
  9.    if((y*y) <= x)
  10.       return 1;
  11.    else
  12.       return 0;
  13. }

  14. int mysqrt01(int x){
  15.    long long L=0,R=(long long)x+1;//[0,x)
  16.    long long ans=0;
  17.    long long mid;
  18.    while(L<R){
  19.       mid=(L+R)/2;
  20.       if(guess(mid,x)){
  21.          ans=mid;
  22.          L=mid+1;
  23.       }else{
  24.          R=mid;
  25.       }
  26.    }
  27.    return (int)ans;
  28. }

  29. int mysqrt02(int x)
  30. {
  31.    int L,R,mid;
  32.    if (x == 0)
  33.       return 0;
  34.    L=1;
  35.    R=x;
  36.    while(1){
  37.       mid = L + (R - L)/2;
  38.       if (mid > x/mid) {
  39.             R = mid - 1;
  40.         } else {
  41.             if (mid + 1 > x/(mid + 1))
  42.                 return mid;
  43.             L = mid + 1;
  44.         }
  45.     }
  46. }


  47. /*
  48. The sequence 1, 2, ... , n has no duplication.
  49. Near the very end, closest step, before while loop, left = mid = right.
  50. In while, If mid < sqrt(x), left = mid + 1 executed, right pointer
  51.    is not moving, and right is the answer.
  52. In while, If mid > sqrt(x), right = mid - 1 executed, right pointer shifts left 1,
  53.    closest to sqrt(x), right is also the answer.
  54. */
  55. int mysqrt03(int x)
  56. {
  57.    int L=1;
  58.    int R=x;
  59.    int mid;
  60.    while(L<=R){
  61.       mid=L+(R-L)/2;
  62.       if(mid==x/mid){
  63.          return mid;
  64.       }else if(mid < x/mid){
  65.          L=mid+1;
  66.       }else{
  67.          R=mid-1;
  68.       }
  69.    }
  70.    return R;
  71. }

  72. int main(int argc, char* argv[])
  73. {
  74.    int T,i,x,result,t;
  75.    freopen("lc0069i.txt","r",stdin);
  76.    scanf("%d",&T);
  77.    t=GetTickCount();
  78.    for(i=0;i<T;i++){
  79.       scanf("%d",&x);
  80.       result=mysqrt03(x);
  81.       printf("Result is %d\n", result);
  82.    }
  83.    t=GetTickCount()-t;
  84.    printf("Time cost %d\n",t);
  85.    return 0;
  86. }
  87. /*
  88. 5
  89. 361
  90. 10000
  91. 16
  92. 2147395599
  93. 2147483647
  94. */









阅读(1462) | 评论(0) | 转发(0) |
0

上一篇:[LeetCode]172

下一篇:[20180310 PRO] Map Completion

给主人留下些什么吧!~~