sysy-data/h-p/sy/h-7-01.sy

45 lines
615 B
Plaintext
Raw Normal View History

2024-08-08 00:23:26 +08:00
float i_buf[10000000];
float o_buf[10000000];
float newtons_sqrt(float n) {
const float prec = 0.0000000001;
if (n <= 0) {
return 0;
}
float x = n;
float r = (n + 1) * 0.5;
float d;
if (x < r) {
d = r - x;
} else {
d = x - r;
}
while (d > prec) {
r = (x + n / x) * 0.5;
if (x < r) {
d = r - x;
} else {
d = x - r;
}
x = r;
}
return r;
}
int main() {
int n = getfarray(i_buf);
starttime();
int i = 0;
while (i < n) {
o_buf[i] = newtons_sqrt(i_buf[i]);
i = i + 1;
}
stoptime();
putfarray(n, o_buf);
return 0;
}