#include <stdio.h>
double fact(int n)
{
if(n<2)
return 1
return n*fact(n-1)
}
double sigama(int a,int b,double (*f)(int))
{
if(a>b)
return 0
return f(a)+sigama(a+1,b,*f)
}
int main()
{
printf("%lf\n",sigama(1,20,fact))
return 0
}
三种方法在同一程序里。#include <stdio.h>
int main()
{
int i,sum=0,sum1=0,sum2=0
for (i=1i<=100i++){
sum=sum+i
if (i%2==1) sum1=sum1+ielse sum2=sum2+i
}
printf("sum=%d odd=%d ev=%d\n",sum, sum1,sum2)
i=0sum=0,sum1=0,sum2=0
do{ i++sum=sum+iif (i%2==0) sum2=sum2+i
} while (i<100)
printf("sum=%d odd=%d ev=%d\n",sum, sum-sum2,sum2)
i=0sum=0,sum1=0,sum2=0
for (i=1i<=100i=i+2) sum1=sum1+i
for (i=2i<=100i=i+2) sum2=sum2+i
sum=sum1+sum2
printf("sum=%d odd=%d ev=%d\n",sum, sum-sum2,sum2)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)