《C语言程序设计现代方法第二版》第二章编程题题解
第一题
编写一个程序,使用printf在屏幕上显示下面图形
#include <stdio.h>
int main()
{
printf(" *\n");
printf(" * \n");
printf(" * \n");
printf(" * * \n");
printf(" * * \n");
printf(" * \n");
return 0;
}
第二题
编写一个计算球体体积程序,其中球体的半径为10m,参考公式v=4/3Πr³。注意,分数4/3应写为4.0f/3.0f(如果写成4/3会产生什么后果?)提示:c语言没有指数运算符,所以需要对r自乘两次来计算。
#include <stdio.h>
int main()
{
int r = 10;
float v;
float pai = 3.14;
v = 4.0f / 3.0f * pai * r * r * r;
printf("球体体积为%f\n", v);
return 0;
}
如果写成4/3会产生什么后果?
结果如下图所示
为什么会出现这种情况?原因是3/4这个运算式是整形运算式。得出来的结果自然为整形,也就是1.所以如果想要得到浮点数也应使用浮点数进行运算。因此需要将3改为3.0f,4改为4.0f。
注:c语言是截断取整。即4.9取整后为4。
4.1取整后为4。
第三题
修改上述程序,使用户可以自行录入球体的半径。
#include <stdio.h>
int main()
{
int r;
float v;
float pai = 3.14;
printf("请输入球体的半径:\n");
scanf("%f", &r);
v = 4.0f / 3.0f * pai * r * r * r;
printf("球体体积为%f\n", v);
return 0;
}
第四题
编写一个程序,使用户输入一个美元数量,然后显示出增加5%税率后的相应金额。格式如下所示:
Enter an amount:100.00
WIth tax added :$105.00
#include <stdio.h>
int main()
{
float x,y;
printf("Enter an amount:");
scanf("%f", &x);
y = x + x * 0.05;
printf("With tax added:$%f",y);
return 0;
}
第五题
编程要求用户输入x的值,然后显示如下多项式的值:
3x⁵+2x⁴-5x³-x²+7x-6
提示:c语言没有指数运算符,所以需要对x自乘来计算其幂。(例如,x*x*x就是x的三次方)
#include <stdio.h>
int main()
{
float x, y;
printf("请输入一个数:\n");
scanf("%f", &x);
y = 3 * x * x * x * x * x + 2 * x * x * x * x - 5 * x * x * x - x * x + 7 * x - 6;
printf("3x⁵+2x⁴-5x³-x²+7x-6=%f", y);
return 0;
}
第六题
修改上题,用如下公式对多项式求值:
((((3x+2)x-5)x-1)x+7)x-6
注意,修改后的程序所需的乘法次数减少了。这种多项式求值方法即Horner法则(Horner's Rule)。
Horner法则即霍纳规则,是将一元n次多项式的求值问题转化为n个一次式的算法。其大大简化了计算过程,即使在现代,利用计算机解决多项式的就求值问题时,霍纳规则依然是最优的算法规则。
霍纳规则是采用最少的乘法运算策略,求多项式A(x)=aₙxⁿ +aₙ₋₁xⁿ+...+a₁x+a₀在x₀的值,该规则是A(x0)=(...((anx0+ an-1)x0+...+ a1)x0+ a0。
就相当于提公因式。3x⁵+2x⁴-5x³-x²+7x-6=((((3x+2)x-5)x-1)x+7)x-6
#include <stdio.h>
int main()
{
float x, y;
printf("请输入一个数:\n");
scanf("%f", &x);
y = ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6;
printf("((((3x+2)x-5)x-1)x+7)x-6=%f", y);
return 0;
}
第七题
编写一个程序,要求用户输入一个美金数量,然后显示出如何用最少的20美元、10美元、5美元、和1美元来付款:
Enter a dollar amount:93
$20 bills:4
$10 bills:1
$5 bills:0
$1 bills:3
提示:将付款金额除以20,确定20美元的数量,然后从付款金额减去20美元的总金额。对其它面值的钞票重复这一操作。确保在程序中始终使用整数值,不要用浮点数。
#include <stdio.h>
int main()
{
int amount, x, y, n, m;
printf("Enter a dollar amount:");
scanf("%d", &amount);
x = amount / 20;
amount = amount - x*20;
y = amount / 10;
amount = amount - y*10;
m = amount / 5;
n = amount - m*5;
printf("$20 bills:%d\n$10 bills:%d\n $5 bills:%d\n $1 bills:%d", x, y, m, n);
return 0;
}
第八题
编程计算第一、第二、第三个月还贷后剩余的贷款金额:
Enter amount of loan:20000.00
Enter interest rate:6.0
Enter monthly payment:386.66
Balance remaining after first payment :$19713.34
Balance remaining after second payment :$19425.25
Balance remaining after third payment :$19135.71
在显示每次还款后的余额时保留两位小数。提示:每个月的贷款余额健全还款金额后,还需要加上贷款余额与月利率的乘积。月利率的计算方法是把用户输入的利率转换成百分数再除以12.
#include <stdio.h>
int main()
{
float loan_amount, interest_rate, monthly_payment,monthly_rate;
float loan_balance, loan_balance1, loan_balance2, loan_balance3;
printf("Enter amount of loan:");
scanf_s("%f", &loan_amount);
getchar();
printf("Enter interest_rate:");
scanf_s("%f", &interest_rate);
getchar();
printf("Enter monthly payment:");
scanf_s("%f", &monthly_payment);
getchar();
monthly_rate = interest_rate / 100 / 12;
loan_balance = loan_amount - monthly_payment;
loan_balance1 = loan_balance + loan_balance * monthly_rate;
loan_balance = loan_balance1 - monthly_payment;
loan_balance2 = loan_balance + loan_balance * monthly_rate;
loan_balance = loan_balance2 - monthly_payment;
loan_balance3 = loan_balance + loan_balance * monthly_rate;
printf("Balance remaining after first payment:%.2f", loan_balance1);
printf("\nBalance remaining after second payment:%.2f", loan_balance2);
printf("\nBalance remaining after third payment:%.2f", loan_balance3);
return 0;
}