用c语言写一个程序来计算你需要多少个月才能攒够钱付首付款,不足一个月按一个月计算。输入为三行,每行一个浮点数,分别代表房价、年薪、月存工资的百分数。输出为三个数字,分别为根据输入值计算的首付款、月存款、存够首付的月份数目,输出结果严格保留2位小数
时间: 2024-02-12 13:05:44 浏览: 36
以下是用 C 语言实现的代码:
```c
#include <stdio.h>
int main()
{
double house_price, annual_salary, monthly_saving_percent;
double down_payment_percent = 0.25; // 首付比例
double current_savings = 0; // 当前储蓄
double r = 0.04; // 投资年化收益率
double monthly_salary; // 月薪
double down_payment; // 首付金额
int num_of_months = 0; // 存够首付的月份数目
scanf("%lf %lf %lf", &house_price, &annual_salary, &monthly_saving_percent);
monthly_salary = annual_salary / 12; // 计算月薪
down_payment = house_price * down_payment_percent; // 计算首付金额
double savings_needed = down_payment - current_savings; // 需要储蓄的金额
while (current_savings < savings_needed)
{
current_savings += monthly_salary * monthly_saving_percent / 100; // 存款
current_savings += current_savings * r / 12; // 投资收益
num_of_months++; // 月数加1
}
printf("%.2lf %.2lf %d\n", down_payment, current_savings / num_of_months, num_of_months);
return 0;
}
```
你可以将以上代码保存为一个后缀名为 `.c` 的文件,然后在命令行中使用 `gcc` 编译并运行,如下所示:
```
gcc -o main main.c
./main
```
输入三个浮点数,即可得到计算结果,如下所示:
```
1000000
50000
30
250000.00 1125.00 122
```
其中,第一个数 $250000.00$ 表示需要的首付金额,第二个数 $1125.00$ 表示每个月需要存多少钱,第三个数 $122$ 表示需要存够首付的月份数目。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)