C 计算阶乘之和

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int jiecheng(int num);
int sum(int n);

main(){
    int num=0;
    scanf("%d",&num);

    //int rs=jiecheng(num);
    //printf("%d",rs);

    int sum1=0;
    int sum2=0;
    int num2;
    if(num%2==0){//是偶数
        num2=num-1;
    }else{
        num2=num-1;
    }

    sum1=sum(num);
    sum2=sum(num2);
    printf("%d",sum1);
    printf("%d",sum2);

    

}

int jiecheng(int num){
    if(num==1){
        return 1;
    }else{
        return num*jiecheng(num-1);
    }

}

int sum(int num){
    int sum=0;
    
    int i=0;
    if(num%2==0){//是偶数  那么第一个需要计算的阶乘从2开始
        i=2;
        while(i<=num){
            sum=sum+jiecheng(i);
            i+=2;
        }


    }else{
        i=1;
        while(i<=num){
            sum=sum+jiecheng(i);
            i+=2;
        }
    
    }
    return sum;

}

 

posted @ 2013-03-13 14:11  cart55free99  阅读(306)  评论(0编辑  收藏  举报