#include<stdio.h>
#include<stdlib.h>
double fun(int n,double x)
{
/***********************Begin************************/
/***********************End*************************/
}
int main()
{
int n;
double s,x;
FILE *in,*out;
printf("输入n和x:");
scanf("%d %lf",&n,&x);
s=fun(n,x);
printf("%lf\n",s);
/*****************/
in=fopen("in2019-2-2-3.dat","r");
out=fopen("out2019-2-2-3.dat","w");
while(!feof(in))
{
fscanf(in,"%d %lf",&n,&x);
fprintf(out,"%lf\n",fun(n,x));
}
fclose(in);
fclose(out);
system("pause");
return 0;
}
供参考:
#include <stdio.h>
#include <stdlib.h>
double fun(int n, double x)
{
/***********************Begin************************/
double s = 0, t = 1.0;
int i, j, k = 0, * a = (int*)malloc(sizeof(int) * n);
a[k++] = 1;
for (i = 1; i <= n; i++) {
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
a[k++] = i;
break;
}
}
}
for (i = 0; i < k; i++) {
t *= x;
i % 2 == 0 ? s += t / a[i] : s -= t / a[i];
}
free(a);
return s;
/***********************End*************************/
}
int main()
{
int n;
double s, x;
FILE* in, * out;
printf("输入n和x:");
scanf("%d %lf", &n, &x);
s = fun(n, x);
printf("%lf\n", s);
/*****************/
in = fopen("in2019-2-2-3.dat", "r");
out = fopen("out2019-2-2-3.dat", "w");
while (!feof(in))
{
fscanf(in, "%d %lf", &n, &x);
fprintf(out, "%lf\n", fun(n, x));
}
fclose(in);
fclose(out);
/***************/
system("pause");
return 0;
}