为什么for循环从n到sqrtn过不了 从1到n就能过
查看原帖
为什么for循环从n到sqrtn过不了 从1到n就能过
1021623
AVLw楼主2023/9/5 00:17
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = n - 1; i > sqrt(n); i--) if (n % i == 0) { 
        cout << i;
        break;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 2; i <= n; i++) if (n % i == 0) { 
        cout << n / i;
        break;
    }
    return 0;
}

从时间复杂度看 根号n不还小一点吗 为什么上面的反而过不了 有没有什么估计时间的方法

2023/9/5 00:17
加载中...