用C语言写一个算静态回收期的代码
时间: 2023-11-15 19:05:47 浏览: 42
以下是一个简单的 C 语言代码示例,用于计算静态投资回收期:
```
#include <stdio.h>
int static_payback_period(int investment, int cash_flows[], int n) {
int cumulative_cash_flows = 0;
int years = 0;
while (cumulative_cash_flows < investment && years < n) {
cumulative_cash_flows += cash_flows[years];
years++;
}
if (cumulative_cash_flows < investment) {
return -1;
} else {
return years;
}
}
int main() {
int investment = 100000;
int cash_flows[] = {20000, 30000, 40000, 50000, 60000, 70000};
int n = sizeof(cash_flows) / sizeof(cash_flows[0]);
int spbp = static_payback_period(investment, cash_flows, n);
if (spbp == -1) {
printf("The investment cannot be fully recovered within the expected cash flow period.\n");
} else {
printf("The static payback period is %d years.\n", spbp);
}
return 0;
}
```
其中,investment 是投资项目的全部成本,cash_flows 是一个包含每年预期现金流入量的数组,n 是数组的长度。函数通过循环累加每年的现金流入量,直到累计现金流量大于等于投资成本,返回所需的年数作为静态投资回收期。如果投资不能在预期现金流期内完全收回,则返回 -1。
示例输出:
```
The static payback period is 3 years.
```