c - 我认为递归在我头顶上运行
问题描述
我创建了这个简单的程序来模拟股票市场的投资,我创建这个程序的主要意图是,
- 如果我在市场上亏损,那么我将以初始投资的 x 倍再次投资(尽管我 + 必须从键盘手动进行),如果我再次亏损,我将投资 x 倍之前的投资;我会这样做,直到我没有赢回来。
那么 x 的 Value 应该是多少才能在最后一次大赢中获得丰厚的利润呢?
- 假设:: 如果赢了,我将赢得之前投资的两倍。
数学上:: 说连续输了 3 场,赢了 4 场。(x=2)
设初始投资为 $1,第二个 2 * $1= $2,第三个 2 * $2= $4;[全3输]
第 4 笔投资 =2 * $4 = $8;[最终获胜] 总投资=$1+$2+$4+$8 =$15
总赢额= 2*$8 = %16 ;(根据上述假设)
利润 = 1 美元。
但是 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:: $
解决方案
推荐阅读
- neo4j - 具有预先计算成本的 Neo4j dijkstra
- mysql - sql中的字符串模式匹配
- python - Django celery - asyncio - 守护进程不允许有孩子
- shell - Cakephp 3.x。从模型执行自定义控制台命令
- javascript - Youtube iframe 没有响应 postMessage 命令
- r - 从 knitr 产品中删除加工百分比
- java - Android 中的 StreamCorruptedException
- netlogo - 使用嵌套询问访问 netlogo 中拥有的特征
- android - 如何阻止此视图填充屏幕?
- c# - 无法在 asp.net 中读取 cookie