已知今年的工业产值为100万元,产值增长率为从键盘输入,请编程计算工业产值过多少年可实现翻一番(即增加一倍)。 参考答案:用符号常量CURRENT表示今年的工业产值为100万元,用变量growRate表示产值增长率,用变量year表示产值翻番所需的年数,则计算年产值增长额的计算公式为: output = output * (1 + growRate) 利用迭代法循环计算,直到output >= 2*CURRENT时为止。当output >= 2*CURRENT时,表示已实现产值翻番。此时,循环被执行的次数year即为产值翻番所需的年数。 **输入格式要求:"%lf" 提示信息:"Input grow rate:" **输出格式要求:"When grow rate is %.0f%%, the output can be doubled after %d years.\n" 程序运行示例如下: Input grow rate:0.08 When grow rate is 8%, the output can be doubled after 10 years.
时间: 2024-03-24 07:40:39 浏览: 133
以下是C语言代码实现:
```c
#include <stdio.h>
#define CURRENT 100.0 // 符号常量表示今年的工业产值为100万元
int main()
{
double growRate;
int year = 0;
double output = CURRENT;
printf("Input grow rate:");
scanf("%lf", &growRate);
while (output < 2 * CURRENT)
{
output *= (1 + growRate);
year++;
}
printf("When grow rate is %.0f%%, the output can be doubled after %d years.\n", growRate * 100, year);
return 0;
}
```
程序运行示例如下:
```
Input grow rate:0.08
When grow rate is 8%, the output can be doubled after 10 years.
```
阅读全文