B. Squares and Cubes
Polycarp likes squares and cubes of positive integers. Here is the beginning of the sequence of numbers he likes: 1, 4, 8, 9, ....
For a given number n, count the number of integers from 1 to n that Polycarp likes. In other words, find the number of such x that x is a square of a positive integer number or a cube of a positive integer number (or both a square and a cube simultaneously).
The first line contains an integer t (1≤t≤201≤t≤20) — the number of test cases.
Then tt lines contain the test cases, one per line. Each of the lines contains one integer n (1≤n≤1091≤n≤109).
For each test case, print the answer you are looking for — the number of integers from 1 to n that Polycarp likes.
6
10
1
25
1000000000
999999999
500000000
4
1
6
32591
32590
23125
思路:该题题意比较容易看得懂,就是给你一个数,让你算出平方和立方都小于这个数的个数是多少,如给出一个10,除去1,2的平方和立方,3的平方都小于10,所以就有4个数。那这道题的解法就相对容易多了,只需要把给出的数开根号,然后再加上开三次方根,加起来就行。但要注意的一点就是,2和3的平方立方也存在重复的情况,所以要减去六次方根。
题解:
#include<bits/stdc++.h> //万能头
using namespace std;
int main(){
int x; //数据个数
cin>>x;
while(x--){ //通过while降低空间复杂度
double n;
cin>>n;
n+=(0.00001);
cout<<int(pow(n,1.0/2))+int(pow(n,1.0/3))-int(pow(n,1.0/6))<<endl; //这里就是思路的实现方式
}
return 0;
}

浙公网安备 33010602011771号