Zero Lee的专栏

求一个正整数的平方根程序实现

求一个正整数的平方根的程序实现:
采用加法递增的方式来代替乘法与N进行比较,递增是按照等差数列的方式。
 1 int square(int n)
 2 {
 3     int tmp = 0;
 4     for (int i = 1; i < n; i++) {
 5         tmp += 2*(i-1)+1;
 6         if (tmp == n)
 7             return i;
 8         continue;
 9     }
10     if (n!=0) {
11         printf("no integer sqare found!\n");
12         tmp = -1;
13     }
14     return tmp;
15 }
16 

posted on 2011-10-19 09:37 Zero Lee 阅读(680) 评论(0)  编辑 收藏 引用 所属分类: Data structure and algorithms


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理