一、f(n)=1+1/2+1/3+1/4+…+1/n
1.程序代码
#include<stdio.h>
int main()
{
int i;
int n;
double sum=0.0;
scanf("%d", &n);
for(i=1;i<=n;i++){
sum += 1.0/i;
}
printf("f(%d) = %f \n", n, sum);
return 0;
}
2.运行结果
二、f(n)=1-1/2+1/3-1/4+…+1/n
1.程序代码
#include<stdio.h>
int main()
{
int i;
int n;
double sum=0.0;
double sign = 1.0;
scanf("%d", &n);
for(i=1;i<=n;i++){
sum += sign/i;
sign=-sign;
}
printf("f(%d) = %f \n", n, sum);
return 0;
}
2.运行结果
二、f(n)=1+1/2-1/3+1/4-…+1/n
1.程序代码
#include<stdio.h>
int main()
{
int i;
int n;
double sum=0.0;
double sign = 1.0;
scanf("%d", &n);
for(i=2;i<=n;i++){
sum += sign/i;
sign=-sign;
}
sum = sum + 1;
printf("f(%d) = %f \n", n, sum);
return 0;
}
2.运行结果
|