|
发表于 2023-4-4 12:57:17
|
显示全部楼层
- #include <stdio.h>
- #include <math.h>
- void exercise0();
- void exercise1();
- void exercise2();
- void exercise3();
- int main()
- {
- exercise0();
- exercise1();
- exercise2();
- exercise3();
- return 0;
- }
- void exercise0()
- {
- int i;
- float profit_a, profit_b, principal, rate_a, rate_b;
- i = 0;
- profit_a = profit_b = principal = 10000;
- rate_a = 0.1;
- rate_b = 0.05;
- while (!(profit_b > profit_a))
- {
- profit_a += principal * rate_a;
- profit_b += profit_b * rate_b;
- i++;
- }
- printf("%d年后,黑夜的投资额超过小甲鱼!\n", i);
- printf("小甲鱼的投资额是:%.2f\n", profit_a);
- printf("黑夜的投资额是:%.2f\n", profit_b);
- }
- void exercise1()
- {
- int year = 0, expense = 50, principal = 400;
- double rate = 0.08;
- while (principal > 0)
- {
- principal -= expense;
- principal += principal * rate;
- year++;
- }
- printf("%d年之后,小甲鱼败光了所有的家产,再次回到一贫如洗……\n", year);
- }
- void exercise2()
- {
- int mark, base;
- double pi, item;
- mark = base = 1;
- pi = 0;
- do
- {
- item = mark / (double)base;
- pi += item;
- mark = -mark;
- base += 2;
- } while (fabs(item) > 1e-8);
- printf("pi = %.7lf\n", 4 * pi);
- }
- int bunny(int i)
- {
- if (i < 3)
- return 2;
- return bunny(i - 1) + bunny(i - 2);
- }
- void exercise3()
- {
- printf("%ld\n", bunny(24));
- }
复制代码 |
|