#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);return0;}
只用注意一个问题,整数的除法结果是整数,所以要用浮点型来转换成小数#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 / ...