递归计数c

时间:2016-01-12 02:37:58

标签: c recursion

美好的一天!我在递归计数方面有一个问题,这个代码已经在网上,但我做了一些额外的代码,我的问题是如何计算程序的IF部分? ,这是代码:

#include <stdio.h>
#include <math.h>

int combinationUtil(int arr[], int data[], int start, int end, int index, int r,float MEAN);

int printCombination(int arr[], int n, int r,float MEAN)
{

int data[r];

combinationUtil(arr, data, 0, n-1, 0, r,MEAN);
}


int combinationUtil(int arr[], int data[], int start, int end, int index, int r,float MEAN)
{
int j,i,a=1,b=0,f=0,ch,total=0;
float sum=0,mean,deviation=0,standard_deviation;



if (index == r){
        int count=0;

    for (j=0; j<r; j++)
    {

       sum=sum+data[j];
        deviation=deviation+pow((MEAN-data[j]),2);
    }

   mean=sum/r;
   standard_deviation=sqrt(deviation/(r-1));

   if(MEAN<=(mean+(2.776*standard_deviation))&& MEAN>=(mean-(2.776*standard_deviation))){
    printf("%d",a); 
    count++;}
  else printf("%15d",b); 
  count=count;

   return ;}



 for (i=start; i<=end && end-i+1 >= r-index; i++)
  {

    data[index] = arr[i];
    combinationUtil(arr, data, i+1, end, index+1, r,MEAN);

  }

  }




int main()
{
int arr[] = {2,4,6,8,10,7,8};
int r = 3,i,count=1,total=0;
int n = sizeof(arr)/sizeof(arr[0]);
float sum=0,MEAN;
for(i=0; i<n; i++)
{
    sum=sum+arr[i];
}
MEAN=sum/n;
printf("MEAN=%f\n\n\n",MEAN);


printCombination(arr,n,r,MEAN));


return 0;
}

这是输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为为了计算IF段,你应该将count变量作为参考参数添加到combinationUtil函数中,这样你就可以得到函数的计数结果。

#include <stdio.h>
#include <math.h>

int combinationUtil(int arr[], int data[], int start, int end, int index, int r, float MEAN, int *count);

int printCombination(int arr[], int n, int r,float MEAN, int *count)
{
    int *data = malloc(sizeof(int) * r);

    combinationUtil(arr, data, 0, n-1, 0, r,MEAN, count);

    free(data);
    return 0;
}


int combinationUtil(int arr[], int data[], int start, int end, int index, int r, float MEAN, int *count)
{
    int j, i, a=1, b=0, f=0, ch, total=0;
    float sum=0, mean, deviation=0, standard_deviation;

    if (index == r) {

        for (j=0; j<r; j++)
        {
             sum=sum+data[j];
                deviation=deviation+pow((MEAN-data[j]),2);
        }

        mean=sum/r;
        standard_deviation=sqrt(deviation/(r-1));

        if(MEAN<=(mean+(2.776*standard_deviation))
            && MEAN>=(mean-(2.776*standard_deviation))){

            printf("%d",a);
            (*count)++;
        }
        else
            printf("%15d",b);

        return 1; // different return value
    }

    for (i=start; i<=end && end-i+1 >= r-index; i++)
    {
        data[index] = arr[i];
        combinationUtil(arr, data, i+1, end, index+1, r,MEAN, count);
    }
    return 0;
}


int main()
{
    int arr[] = {2,4,6,8,10,7,8};
    int r = 3, i, count=0, total=0;
    int n = sizeof(arr)/sizeof(arr[0]);
    float sum = 0, MEAN;
    for(i=0; i<n; i++)
    {
        sum = sum + arr[i];
    }
    MEAN = sum / n;
    printf("MEAN=%f\n\n\n",MEAN);

    printCombination(arr, n, r, MEAN, &count);

    printf("\ncount = %d", count);

    return 0;
}

编辑:代码已完成,仅在C中而不是在C ++中。