第二章 练习题
第一题
没有警告信息
第二题
(1)指令: #include<stdio.h>
语句:带分号 ;
(2)Parkinson's Law
Work expands so as to fill the time
available for its completion.
第三题
编程题: 略
第四题
无
第五题
无
第六题
无
第七题
无
第八题
无
第二章 编程题
chapter 2 -1
编写一个程序,使用printf在屏幕上显示下面的图形∶
#include <stdio.h>
int main()
{
int i ;
int j ;
for ( i = 0 ; i< 6 ; i++)
{
if ( i == 0)
{
for ( j = 0 ; j < 8 ; j ++)
{
if (j == 7)
{
printf("*\n");
}
else
{
printf(" ");
}
}
}
if ( i ==1 )
{
for( j = 0 ; j < 8 ; j++)
{
if (j == 6)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
if( i ==2)
{
for( j = 0 ; j < 8 ; j++)
{
if (j == 5)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
if ( i ==3 )
{
for( j = 0 ; j < 8 ; j++)
{
if (j == 4 || j == 0 )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
if ( i == 4 )
{
for( j = 0 ; j < 8 ; j++)
{
if (j == 3 || j ==1 )
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
if (i ==5 )
{
for( j = 0 ; j < 8 ; j++)
{
if (j == 2)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
}
这道题的编写,我才用了很暴力的方式,所以代码有点冗杂,虽然有简单方法进行稍微稍微的处理,不过我觉得还是比较复杂,所以就QAQ…
chapter 2 - 2 和chapter 2 - 3
2.编写一个计算球体体积的程序,其中球体半径为10 m,参考公式v=4/3元³。注意,分数4/3应写为
4.0f/3.0f。(如果分数写成4/3会产生什么结果?)提示; C语言没有指数运算符,所以需要对r 自乘两次来计算r。
3.修改上题中的程序,使用户可以自行录入球体的半径。
注意的是,圆周率需要自己定义一个常量, 2和3相比,就是3增加一个输入语句。
#include <stdio.h>
#define p 3.141592
int main()
{
const int r = 10 ;
double volume = 0.0 ;
volume = 4.0/3.0 * p * r * r * r ;
printf("%.6f" , volume) ;
return 0 ;
}
```c
//chapter 2 - 3
#include <stdio.h>
#define p 3.141592
int main()
{
int r = 0 ;
double volume = 0.0 ;
scanf("%d", &r) ;
volume = 4.0/3.0 * p * r * r * r ;
printf("%.6f" , volume) ;
return 0 ;
}
我的输出格式是采用保留数据后六位的形式。
chapter 2 -4
4.编写一个程序,要求用户输入一个美金数量,然后显示出增加5%税率后的相应金额。格式如下所示∶
#include <stdio.h>
int main()
{
double amount ;
scanf("%lf", &amount) ;
double tax ;
tax = amount + amount * 0.05 ;
printf("%.2lf", tax ) ;
return 0;
}
chapter 2 - 5
#include <stdio.h>
int main()
{
double x ;
scanf("%lf", &x ) ;
double S = 0.00 ;
S = 3 * x * x * x * x * x + 2 * x * x * x * x - 5 * x * x * x - x * x + 7 * x - 6 ;
printf("%.3lf", S) ;
return 0 ;
}
chapter 2 -6
#include <stdio.h>
int main()
{
double x ;
scanf("%lf", &x) ;
double S = 0.00 ;
S = ((((3 * x + 2) * x - 5) * x -1) * x + 7 ) * x - 6 ;
printf("%.2lf", S);
return 0 ;
}
chapter 2 -7
#include <stdio.h>
int main()
{
int amount ;
scanf("%d", &amount) ;
int amount_20 = 0;
int amount_10 = 0 ;
int amount_5 = 0 ;
int amount_1 = 0;
amount_20 = amount / 20 ;
if ( amount % 20 != 0 )
{
int y_1 = amount % 20 ;
amount_1 = y_1 / 10 ;
if ( y_1 % 10 != 0)
{
int y_2 = y_1 % 10;
amount_5 = y_2 / 5;
if (y_2 % 5 != 0)
{
int y_3 = y_2 % 5;
amount_1 = y_3 / 1;
}
}
}
printf("$20 bills : %d \n", amount_20);
printf("$10 bills : %d \n", amount_10);
printf("$5 bills : %d \n", amount_5) ;
printf("$1 bills : %d ", amount_1) ;
return 0 ;
}
这貌似是一道常用的算法,叫做啥方法,我忘记了,离散数学讲过QAQ
chapter 2 -8
#include <stdio.h>
int main()
{
double loan, rate , payment ;
printf("Enter amount of loan: ");
scanf("%lf", &loan) ;
printf("Enter interest rate : ");
scanf("%lf", &rate) ;
printf("Enter monthly payment : ");
scanf("%lf", &payment) ;
double Balance_1 , Balance_2 , Balance_3 ;
Balance_1 = (loan - payment) + loan * rate / 1200 ;
Balance_2 = (Balance_1 - payment) + Balance_1 * rate / 1200 ;
Balance_3 = (Balance_2 - payment) + Balance_2 * rate / 1200 ;
printf("Balance remaining after first payment : $%.2lf\n", Balance_1) ;
printf("Balance remaining after first payment : $%.2lf\n", Balance_2) ;
printf("Balance remaining after first payment : $%.2lf", Balance_3) ;
return 0 ;
}