算法1
时间复杂度
$O(sqrt^3(n))$
C++ 代码
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int n;
cin >> n;
int l = 0, r = sqrt(n);
for(int i = 0;i <= r;i++){
for(int j =i;j <= r;j++){
int low = j, high = sqrt(n);
int temp = i * i + j * j;
while (low < high){ // 双指针
if(temp + low * low + high * high > n) high--;
else if (temp + low * low + high * high < n) low++;
else {cout << i << " " << j << " " << low << " " << high << endl;
return 0;
}
}
}
}
}