《C语言程序设计现代方法(K.N.King著)》第2章课后编程题答案及部分内容的分析。
1.编写一个程序,使用printf在屏幕上显示下面的图形。
/*
*编写一个程序,使用printf在屏幕上显示下面的图形
*/
#include<stdio.h>
int main(void)
{
printf(" *\n");
printf(" *\n");
printf(" *\n");
printf("* *\n");
printf(" * *\n");
printf(" *\n");
return 0;
}
2.编写一个计算球体体积的程序,其中球体半径为10m。
这一题要注意int型数据在经过运算后还是int型,例如4/3的结果为1,所以我们要声明float型的变量,以便对其操作。
/*
*编写一个计算球体体积的程序,其中球体半径为10m,参考公式v=4/3PIr^3.
* 注意,分数4/3应写成4.0f/3.0f。
*/
#include<stdio.h>
#include<math.h>
#define PI 3.14
int main(void)
{
float r, v;
r = 10.0f;
v = (4.0f / 3.0f) * PI * r * r * r;
printf("The volume is %.2f\n",v);
return 0;
}
3.修改上题中的程序,使用户可以自行输入球体半径。
这里给个小提示,在Visual Studio编译时,使用 scanf(); 会报错,可以改为 scanf_s();
/*
*修改上题的程序,使用户可以自行输入球体的半径
*/
#include<stdio.h>
#define PI 3.14
int main(void)
{
float r;
float v;
printf("Enter the radius:");
scanf("%f",&r);
v = (4.0f / 3.0f) * PI * r * r * r;
printf("The volume is %.2f\n", v);
return 0;
}
4.编写一个程序,要求用户输入一个美元数额,然后显示出增加5%税率后的相应金额。
声明变量的类型要考虑清楚,在编写中要注意书本题目的格式要求。
/*
*编写一个程序,要求用户输入一个美元数额,然后先输出增加5%税率后的相应金额。
*/
#include<stdio.h>
int main(void)
{
float amount, money;
printf("Enter an amount:");
scanf("%f",&amount);
money = amount + (amount * 0.05);
printf("With tax added:$%.2f\n",money);
return 0;
}
5.编程要求用户输入x的值,然后求出如下多项式的值。
/*
*编程要求用户输入x的值,然后显示如下多项式的值
*/
#include<stdio.h>
int main(void)
{
float x, y;
printf("Enter the \"x\":");
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^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 == %.2f\n",y);
return 0;
}
6.修改上题,用如下公式对多项式求值:((((3x + 2)x - 5)x - 1)x + 7)x - 6
这道题采用了著名的秦九韶算法,也称为霍纳(Homer)法则。在计算多项式时,通过减少乘法的次数来降低程序计算、运行的时间。(对计算机来说,相对于乘法而言,进行加法运算所需的时间很短)一般地,一元n次多项式的求值需要经过(n+1)*n/2次乘法和n次加法,而秦九韶算法只需要n次乘法和n次加法。当处理大量数据时,这将大大简化运算过程。
/*
*修改上题,用如下公式对多项式求值
*/
#include<stdio.h>
int main(void)
{
float x, y;
printf("Enter the \"x\":");
scanf("%f", &x);
y = ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6;
printf("3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 == %.2f\n", y);
return 0;
}
7.编写一个程序,要求用户输入一个美元数额,然后显示出如何用最少的张数的20美元,10美元、5美元和1美元钞票来付款。
将付款金额除以20,确定20美元钞票的数量,然后从付款金额中减去20美元的总金额,多次操作,便可解出。
/*
*编写一个程序,要求用户输入一个美元数额,然后显示如何用最少张数的20美元、10美元、5美元和1美元钞票来付款
*/
#include<stdio.h>
int main(void)
{
int amount;
printf("Enter a dollar amount:");
scanf("%d",&amount);
printf("$20 bills: %d\n",amount/20);
/*剩余的美元数额为:原有的美元数额,减去,已用大面额钞票支付的数额*/
amount = amount - amount / 20 * 20;
printf("$10 bills: %d\n",amount/10);
amount = amount - amount / 10 * 10;
printf(" $5 bills: %d\n",amount/5);
amount = amount - amount / 5 * 5;
printf(" $1 bills: %d\n",amount/1);
amount = amount - amount / 1 * 1;
if (amount != 0) printf("ERROR!\n");
return 0;
}
8.编程计算第一、第二、第三个月还贷后剩余的贷款金额。
注意书本题目中给出的利息计算方式,每月还贷之后,都会产生新的利息。
/*
*编程计算第一、第二、第三个月还贷后剩余的贷款金额:
*在显示每次还款后的金额时保留两位小数。
*/
#include<stdio.h>
int main(void)
{
float amount, rate, monthly_payment;
printf("Enter amount of loan:");
scanf("%f",&amount);
printf("Enter interest rate:");
scanf("%f",&rate);
printf("Enter monthly payment:");
scanf("%f", &monthly_payment);
printf("Balance remaining after first payment: $%.2f\n", amount - monthly_payment);
/*剩余的贷款金额为:原贷款金额减去上月还款后,加上一个月产生的利息*/
amount = (amount - monthly_payment) * (1 + rate / 100 / 12);
printf("Balance remaining after second payment: $%.2f\n", amount - monthly_payment);
amount = (amount - monthly_payment) * (1 + rate / 100 / 12);
printf("Balance remaining after third payment: $%.2f\n", amount - monthly_payment);
return 0;
}