首页 > 解决方案 > 我认为递归在我头顶上运行

问题描述

我创建了这个简单的程序来模拟股票市场的投资,我创建这个程序的主要意图是,

那么 x 的 Value 应该是多少才能在最后一次大赢中获得丰厚的利润呢?

数学上:: 说连续输了 3 场,赢了 4 场。(x=2)

但是 C 中的递归让我很吃惊,我无法处理它。请帮帮我。

代码:

#include <stdio.h>
#include <stdint.h>
#include <time.h>

int deposit;
int value;

int RandomNumber(int lower, int upper) //this function will act as unpredectable behavior of Market.
{
    int num = (rand() %
               (upper - lower + 1)) +
              lower;
    return num;
}

int Market(int value)
{
    int MarketBehaviour;
    if (value <= 0)
    {
        printf("Invalid fund....\n");
        Invest();   //It will let me Put value again if i entered wrong value.
    }
    else if (value > 0 && value <= deposit)
    {

        srand(time(0));
        MarketBehaviour = RandomNumber(0, 1);
        if (MarketBehaviour == 1) // 1 represents won in Investment
        {
            printf("## TradeResult::Won ## \n\n");
            deposit = deposit - value + value * 2;
            printf("Total left in Deposit:: $%d\n", deposit);
        }
        else // else,in this case 0,represents loss in Investment.
        {
            printf("## TradeResult::Loss ##\n\n");
            while (MarketBehaviour == 0 && deposit >= value)
            {

                deposit = deposit - value;

                printf("Total left In Deposit:: $%d \n", deposit);
                printf("Investment in Current Stage:: $%d \n", value);
                Invest(); //this function will let me invest again if i loss.
            }
        }
    }
}
int Invest()
{
    printf("Enter value to Invest from deposit:: $");
    scanf("%d", &value);
    Market(value);
}

int main()
{
    printf("Enter money to deposit:: $");
    scanf("%d", &deposit);
    printf("\n");
    Invest();
}

输出:

        try.c: In function 'RandomNumber':
        try.c:28:16: warning: implicit declaration of function 'rand'; did you mean 'rewind'? [-Wimplicit-function-declaration]
             int num = (rand() %
                        ^~~~
                        rewind
        try.c: In function 'Market':
        try.c:40:9: warning: implicit declaration of function 'Invest' [-Wimplicit-function-declaration]
                 Invest(); //It will let me Put value again if i entered wrong value.
                 ^~~~~~
        try.c:45:9: warning: implicit declaration of function 'srand'; did you mean 'scanf'? [-Wimplicit-function-declaration]
                 srand(time(0));
                 ^~~~~
                 scanf
    
    Enter money to deposit:: $100 
    
    Enter value to Invest from deposit:: $1
    ## TradeResult::Loss ##
    
    Total left In Deposit:: $99
    Investment in Current Stage:: $1
    Enter value to Invest from deposit:: $2
    ## TradeResult::Won ## 
    
    Total left in Deposit:: $101   (In my knowlodge , program was supposed to stop here)
    Total left In Deposit:: $100
    Investment in Current Stage:: $1
    Enter value to Invest from deposit:: $4
    ## TradeResult::Won ## 
    
    Total left in Deposit:: $104
    Total left In Deposit:: $103
    Investment in Current Stage:: $1`
    Enter value to Invest from deposit:: $ 

标签: crecursion

解决方案


推荐阅读