#include <stdio.h> int main() { int n; float sum = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { //printf("1/%d\n", i);//显示测试 sum += 1 / (float)i;//整数1/2=0,所以强制转换为浮点型 } printf("%lf", sum); return 0; }
示例: