突发奇想股票增长指数

假如拿出本金4万买股票,一个月买入一次卖出一次算交易1次,每一手股票交易一次赚100元每个月交易一次,每手股票本金2000元第一个月能买20手。将赚到的钱全部用来买股票且从来不亏损,那么3年之内能赚多少?

下面写了个程序来计算这个问题。当然是越到后面赚的钱越多,第36个月竟可以买108手,本金从4万增长到21万多。这就是复利的威力!

# include<stdio.h>
static int total_sum = 0;
static int sum_yu = 0;
static int inc_roundlot = 20;
// 只取余是不对的,假如41手,那么41*100=4100。取余只能得到100,
// 实际上可以增加2手股票再余100元。因此这里用递归处理此问题。
void recursion(int d_sum){
if(d_sum - 2000 >= 2000){
inc_roundlot++;
recursion(d_sum - 2000);
}
}
/**
* sum_yu
* inc_roundlot 股票交易手数
* */
void count_sum(int month){
int roundlot = 0, d_sum = 0;
roundlot = inc_roundlot + month;
d_sum = roundlot * 100; // 每个月每一手交易一次赚100元
printf("roundlot = %d, d_sum = %d\n", roundlot, d_sum);
recursion(d_sum);
sum_yu += d_sum % 2000;
if(sum_yu >= 2000){
sum_yu -= 2000;
inc_roundlot++;
}
total_sum = roundlot * 2000;
printf("total_sum = %d, ", total_sum);
total_sum += sum_yu;
printf("total_sum = %d\n\n", total_sum);
}
int main(void)
{
for(int i = 0; i < 36; i++){
printf("month: %d, ", i);
count_sum(i);
month++;
}
return 0;
}

运行结果:

month: 0, roundlot = 20, d_sum = 2000
total_sum = 40000, total_sum = 40000
month: 1, roundlot = 21, d_sum = 2100
total_sum = 42000, total_sum = 42100
month: 2, roundlot = 22, d_sum = 2200
total_sum = 44000, total_sum = 44300
month: 3, roundlot = 23, d_sum = 2300
total_sum = 46000, total_sum = 46600
month: 4, roundlot = 24, d_sum = 2400
total_sum = 48000, total_sum = 49000
month: 5, roundlot = 25, d_sum = 2500
total_sum = 50000, total_sum = 51500
month: 6, roundlot = 26, d_sum = 2600
total_sum = 52000, total_sum = 52100
month: 7, roundlot = 28, d_sum = 2800
total_sum = 56000, total_sum = 56900
month: 8, roundlot = 29, d_sum = 2900
total_sum = 58000, total_sum = 59800
month: 9, roundlot = 30, d_sum = 3000
total_sum = 60000, total_sum = 60800
month: 10, roundlot = 32, d_sum = 3200
total_sum = 64000, total_sum = 64000
month: 11, roundlot = 34, d_sum = 3400
total_sum = 68000, total_sum = 69400
month: 12, roundlot = 35, d_sum = 3500
total_sum = 70000, total_sum = 70900
month: 13, roundlot = 37, d_sum = 3700
total_sum = 74000, total_sum = 74600
month: 14, roundlot = 39, d_sum = 3900
total_sum = 78000, total_sum = 78500
month: 15, roundlot = 41, d_sum = 4100
total_sum = 82000, total_sum = 82600
month: 16, roundlot = 43, d_sum = 4300
total_sum = 86000, total_sum = 86900
month: 17, roundlot = 45, d_sum = 4500
total_sum = 90000, total_sum = 91400
month: 18, roundlot = 47, d_sum = 4700
total_sum = 94000, total_sum = 94100
month: 19, roundlot = 50, d_sum = 5000
total_sum = 100000, total_sum = 101100
month: 20, roundlot = 52, d_sum = 5200
total_sum = 104000, total_sum = 104300
month: 21, roundlot = 55, d_sum = 5500
total_sum = 110000, total_sum = 111800
month: 22, roundlot = 57, d_sum = 5700
total_sum = 114000, total_sum = 115500
month: 23, roundlot = 60, d_sum = 6000
total_sum = 120000, total_sum = 121500
month: 24, roundlot = 63, d_sum = 6300
total_sum = 126000, total_sum = 127800
month: 25, roundlot = 66, d_sum = 6600
total_sum = 132000, total_sum = 132400
month: 26, roundlot = 70, d_sum = 7000
total_sum = 140000, total_sum = 141400
month: 27, roundlot = 73, d_sum = 7300
total_sum = 146000, total_sum = 146700
month: 28, roundlot = 77, d_sum = 7700
total_sum = 154000, total_sum = 154400
month: 29, roundlot = 81, d_sum = 8100
total_sum = 162000, total_sum = 162500
month: 30, roundlot = 85, d_sum = 8500
total_sum = 170000, total_sum = 171000
month: 31, roundlot = 89, d_sum = 8900
total_sum = 178000, total_sum = 179900
month: 32, roundlot = 93, d_sum = 9300
total_sum = 186000, total_sum = 187200
month: 33, roundlot = 98, d_sum = 9800
total_sum = 196000, total_sum = 197000
month: 34, roundlot = 103, d_sum = 10300
total_sum = 206000, total_sum = 207300
month: 35, roundlot = 108, d_sum = 10800
total_sum = 216000, total_sum = 216100
month: 36, roundlot = 114, d_sum = 11400
total_sum = 228000, total_sum = 229500

作者:yangsail

出处:https://www.cnblogs.com/yangsail/p/18327563

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   仰望星空_22  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题