There are two ways to ask this question: spelling and math
Let’s talk about math practice:
Since the number of square digits is a continuous number, we open the root number in the range, move to the integer area in the middle, and then determine the length.
Here is the colon method:
We can store all square numbers first, and then find the two search elements in the left and right intervals. Use low_bound and overbound
Math: SQRT®-SQRT(L) +1
2 items: overbound(a, a + n, r) – down_bound (a, a + n, l)
Math method:
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int T;cin >> T;
while (T--) {
int l, r;cin >> l >> r;
cout << floor(sqrt(1.0 * r)) - ceil(sqrt(1.0 * l)) + 1 << endl;
}
return 0;
}
Then there are two points:
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int main() {
int len = 0;
for (int i = 0;i * i <= 1e9;i++)
a[len++] = i * i;
int T;cin >> T;
while (T--) {
int l, r;
cin >> l >> r;
cout << upper_bound(a, a + len, r) - lower_bound(a, a + len, l) << endl;
}
return 0;
}