-
Notifications
You must be signed in to change notification settings - Fork 0
/
200116-1.cpp
41 lines (38 loc) · 1.1 KB
/
200116-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// https://leetcode-cn.com/problems/sqrtx/
#include <cstdio>
class Solution {
public:
int mySqrt(int x) {
if (x <= 1) return x;
for (int c = 1; c <= x; ++c) {
int d = x / c;
if (c == d) return c;
if (c > d) return c - 1;
}
return -1;
}
};
int main()
{
Solution s;
printf("%d\n", s.mySqrt(4)); // answer: 2
printf("%d\n", s.mySqrt(8)); // answer: 2
printf("%d\n", s.mySqrt(0)); // answer: 0
printf("%d\n", s.mySqrt(1)); // answer: 1
printf("%d\n", s.mySqrt(2)); // answer: 1
printf("%d\n", s.mySqrt(3)); // answer: 1
printf("%d\n", s.mySqrt(4)); // answer: 2
printf("%d\n", s.mySqrt(5)); // answer: 2
printf("%d\n", s.mySqrt(6)); // answer: 2
printf("%d\n", s.mySqrt(7)); // answer: 2
printf("%d\n", s.mySqrt(8)); // answer: 2
printf("%d\n", s.mySqrt(9)); // answer: 3
printf("%d\n", s.mySqrt(10)); // answer: 3
printf("%d\n", s.mySqrt(11)); // answer: 3
printf("%d\n", s.mySqrt(12)); // answer: 3
printf("%d\n", s.mySqrt(13)); // answer: 3
printf("%d\n", s.mySqrt(14)); // answer: 3
printf("%d\n", s.mySqrt(15)); // answer: 3
printf("%d\n", s.mySqrt(16)); // answer: 4
return 0;
}