數列定義:首項為 n,後續各項為前一項的平方根。求前 m 項和,精度保留小數點後兩位。
n (n<10000)、m (m<1000)%.2lfInput: 81 4 項1: 81 項2: √81 = 9 項3: √9 = 3 項4: √3 ≈ 1.732 和 = 81 + 9 + 3 + 1.732 = 94.732 → 94.73 Input: 2 2 項1: 2 項2: √2 ≈ 1.414 和 = 3.414 → 3.41
n, m 直到 EOFsum = n,current = nm-1 次:current = sqrt(current),累加到 sumprintf("%.2lf\n", sum)#include <stdio.h>
#include <math.h>
int main() {
int m, i;
double n, sum;
while (scanf("%lf %d", &n, &m) != EOF) {
sum = n;
for (i = 1; i < m; i++) {
n = sqrt(n); // 更新 n 為平方根
sum += n;
}
printf("%.2lf\n", sum);
}
return 0;
}
sqrt 需 math.h,參數/返回值均為 doublen 避免額外變量%.2lf 自動四捨五入,符合題目精度要求數列單調遞減並快速收斂至 1。當 n=1 時後續全為 1,和為 m。大 n 時前幾項貢獻主要值。