#include <stdio.h>
#include <String.h>
void oushu(int a)
{
int i;
double sum = 0.0;
for (i =2; i<=a; i+=2)
{
sum += 1.0/i;
printf("1/%d ",i);
}
printf ("\n值为:%lf\n", sum);
}
void jishu(int b)
{
int i;
double sum = 0.0;
for (i = 1; i <=b; i+=2)
{
sum += 1.0/i;
printf("1/%d ",i);
}
printf ("\n值为:%lf\n", sum);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
int n;
printf ("请输入n的值:\n");
scanf ("%d",&n);
if (n % 2 == 0)
{
oushu(n);
}
if(n % 2 == 1){
jishu(n);
}
return 0;
}
}
|