码迷,mamicode.com
首页 > 其他好文 > 详细

计蒜客 X的平方根

时间:2016-06-02 13:34:04      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:

X的平方根

设计函数int sqrt(int x),计算x的平方根。

格式:

   输入一个数x,输出它的平方根。直到碰到结束符号为止。

   千万注意:是int类型哦~

   输入可以如下操作:

while(cin>>x)

或者

while(scanf("%d", &x) != EOF)

 

样例输入

1
2
3
4
5
6
7
8
9

样例输出

1
1
1
2
2
2
2
2
3

 1 #include "iostream"
 2 #include "math.h"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int x;
 8     while (cin >> x)
 9     {
10         if (x == 1)
11             cout << 1 << endl;
12         //for (int i = x; i <= 1; i++)
13         //{
14         //    if ((x >= (i*i)) && (x < (i + 1)*(i + 1)))
15         //        cout << i << endl;
16         //}
17 
18         else cout << (int)sqrt(x)<<endl;
19     }
20 }    

 

计蒜客 X的平方根

标签:

原文地址:http://www.cnblogs.com/SeekHit/p/5552614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!