回覆列表
-
1 # 熱心市民小劉
-
2 # 看我不爽就滾363
#include<stdio.h>
int main()
{
int n;
double sum=0;//記錄總和初始值為0
printf("輸入n的值:");
scanf("%d",&n);
for(int i=1;i<=n;i++)sum+=1.0/i;
printf("前n項之和為:%lf\n",sum);
return 0;
}
執行透過,可用.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float polynomial(int n){
float sum=0;
int i;
for(i=1;i<=n;i++){
if(i%2 == 1)
sum += 1.0/i;
if(i%2 == 0)
sum -= 1.0/i;
}
return sum;
}
int main(int argc,char *argv[]) {
int t;
float sum;
scanf("%d",&t);
sum = polynomial(t);
printf("%f",sum);
return EXIT_SUCCESS;
}