判断一个数是否是某个数的平方

出处:http://www.dewen.org/q/620

#include <stdio.h>

int judge(int num)
{
  int min = 1;
  if(num < 0)
  {
    return 0;
  }
  else if(num == 0 || num == 1)
  {
    return 1;
  }
  else
  {
    int current = 2;
    while(1)
    {
      if(current * current < num)
      {
    min = current;
    current = min * 2;
      }
      else if(current * current == num)
      {
    return 1;
      }
      else if(current > min + 1)
      {
    current = ((min+current)) / 2;
      }
      else
      {
    return 0;
      }
      printf("%d", current);
    }
  }
}

int main()
{
  printf("%d", judge(8));
}

 

posted @ 2013-02-20 18:37  wouldguan  阅读(448)  评论(0编辑  收藏  举报