北郵機試——special數

  • 題意
    設一個數既是平方數又是立方數,那稱它爲special數。在輸出1-n中有多少special數。
  • 思路
    一開始沒有轉過彎來,長時間沒寫題,想的是兩個set,一個加入平方數一個加入立方數,然後判斷公共數。
    後來想到special數是六次方數, 於是很簡單了。
  • 代碼
#include "bits/stdc++.h"
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mk make_pair
typedef long long ll;
const int maxn = 5e3 + 5;
int t;
ll n;
int main(){
    freopen("1.txt","r",stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>t;
    while (t--) {
        int ans = 0;
        cin>>n;
        for (ll i = 1; i <= n; i++) {
            if (pow(i,6) <= n) {
                ans ++;
            }else break;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  • 遇到的問題
    不用判斷一個數開六次方滿足條件與否,直接從1開始六次方,只要小於等於n即可。n雖然是1e9以上,但是六次方,很小。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章