C语言用下面贪婪算法如何编程实现

1个回答

  • #include

    #include

    int little(double n){

    static int i=0;

    if(n==0.0){

    return;

    }else{

    if(n>=1.0){

    ++i;

    printf("n=%fn",(n-1));

    little(n-1.0);

    }else if(n>=0.5){

    ++i;

    printf("n=%fn",(n-0.5));

    little(n-0.5);

    }else if(n>=0.1){

    ++i;

    printf("n=%fn",(n-0.1));

    little(n-0.1);

    }

    }

    return i;

    }

    int main()

    {

    printf("Hello world=%d!n",little(2.7));

    return 0;

    }