明解C语言 入门篇 第五章练习题
- 5-1
- 5-2
- 5-3
- 5-4
- 5-5
- 5-6
- 5-7
- 5-8
- 5-9
- 5-10
- 5-11
- 5-12
5-1
#include
int main()
{
int i;
int v[5];
for (i = 0;i < 5;i ++)
{
v[i] = i;
}
for (i = 0;i < 5;i ++)
{
printf("v[%d] = %d\n",i,v[i]);
}
return 0;
}
5-2
#include
int main()
{
int i;
int v[5];
for (i = 0;i < 5;i ++)
{
v[i] = 5 - i;
}
for (i = 0;i < 5;i ++)
{
printf("v[%d] = %d\n",i,v[i]);
}
return 0;
}
5-3
#include
int main()
{
int i;
int v[5] = {
5,4,3,2,1};
for (i = 0;i < 5;i ++)
{
printf("v[%d] = %d\n",i,v[i]);
}
return 0;
}
5-4
#include
int main()
{
int i;
int a[5] = {
17, 23, 36};
int b[5];
for (i = 0;i < 5;i ++)
{
b[4-i] = a[i];
}
puts(" a b");
puts("----------");
for (i = 0;i < 5;i ++)
{
printf("%4d%4d\n",a[i],b[i]);
}
return 0;
}
5-5
#include
#define NUMBER 7
int main()
{
int i;
int x[NUMBER];
for (i = 0;i < NUMBER;i ++)
{
printf("x[%d]:",i);
scanf("%d",&x[i]);
}
for (i = 0;i < NUMBER / 2;i ++)
{
int temp = x[i];
x[i] = x[NUMBER - 1 - i];
x[NUMBER - 1 - i] = temp;
}
puts("倒序排列了。");
for (i = 0;i < NUMBER;i ++)
{
printf("x[%d] = %d\n",i,x[i]);
}
return 0;
}
5-6
#include
int main() {
int b;
double a;
a = b = 1.5;
printf("a = %f,b = %d\n",a,b);
return 0;
}
5-7
#include
#define NUMBER 4
int main()
{
int num;
int i;
int v[NUMBER];
printf("数据个数:");
scanf("%d",&num);
for (i = 0;i < NUMBER;i ++)
{
printf("%d号:",i + 1);
scanf("%d",&v[i]);
}
putchar('{');
for (i = 0;i < NUMBER;i ++)
{
printf("%d",v[i]);
if (i != NUMBER - 1)
{
putchar(',');
}
}
putchar('}');
return 0;
}
5-8
#include
#define NUMBER 80
int main()
{
int i, j;
int num;
int tensu[NUMBER];
int bunpu[11] = {
0};
printf("请输入学生人数:");
do {
scanf("%d",&num);
if (num < 1 || num > NUMBER)
{
printf("\a请输入1~%d之间的人数。",NUMBER);
}
}while (num < 1 || num > NUMBER);
printf("请输入%d个学生的成绩。\n",num);
for (i = 0;i < num;i ++)
{
printf("%2d号:",i + 1);
do {
scanf("%d",&tensu[i]);
if (tensu[i] < 0 || tensu[i] > 100)
{
printf("\a请输入1~100的数。");
}
}while (tensu[i] < 0 || tensu[i] > 100);
bunpu[tensu[i] / 10]++;
}
puts("----分布图----");
for (i = 0;i <= 9;i ++)
{
printf("%3d~%3d:",i * 10,i * 10 + 9);
for (j = 0;j < bunpu[i];j ++)
{
putchar('*');
}
putchar('\n');
}
printf(" 100:");
for (j = 0;j < bunpu[10];j ++)
{
putchar('*');
}
putchar('\n');
return 0;
}
5-9
#include
#define NUMBER 80
int main()
{
int i, j;
int num;
int tensu[NUMBER];
int bunpu[11] = {
0};
int max;
printf("请输入学生人数:");
do {
scanf("%d",&num);
if (num < 1 || num > NUMBER)
{
printf("\a请输入1~%d之间的人数。",NUMBER);
}
}while (num < 1 || num > NUMBER);
printf("请输入%d个学生的成绩。\n",num);
for (i = 0;i < num;i ++)
{
printf("%2d号:",i + 1);
do {
scanf("%d",&tensu[i]);
if (tensu[i] < 0 || tensu[i] > 100)
{
printf("\a请输入1~100的数。");
}
}while (tensu[i] < 0 || tensu[i] > 100);
bunpu[tensu[i] / 10]++;
}
max = bunpu[0];
for (i = 1;i < 11;i ++)
{
if (max < bunpu[i])
{
max = bunpu[i];
}
}
puts("\n--------------------------------分布图---------------------------------");
for (i = 0;i < max;i ++)
{
for (j = 0;j < 11;j ++)
{
if (bunpu[j] >= max - i)
{
printf(" * ");
} else{
printf(" ");
}
}
printf("\n");
}
puts("----------------------------------------------------------------------");
puts(" 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 1 0 0");
return 0;
}
5-10
#include
#define a 3
#define b 4
int main()
{
int h1[a][b];
int h2[b][a];
int i, j;
puts("请输入3行4列矩阵:");
for (i = 0;i < a;i ++)
{
printf("第%d行:",i+1);
for (j = 0;j < b;j ++)
{
scanf("%d",&h1[i][j]);
}
putchar('\n');
}
puts("请输入4行3列矩阵:");
for (i = 0;i < b;i ++)
{
printf("第%d行:",i+1);
for (j = 0;j < a;j ++)
{
scanf("%d",&h2[i][j]);
}
putchar('\n');
}
puts("两矩阵的乘积为:");
for (i = 0;i < a;i ++)
{
for (j = 0;j < a;j ++)
{
int ptd = 0;
for (int x = 0;x < b;x ++)
{
ptd += h1[i][x]*h2[x][j];
}
printf("%5d",ptd);
}
putchar('\n');
}
return 0;
}
5-11
#include
#define a 6
#define b 2
int main()
{
int grade[a][b];
int i, j;
puts("请输入成绩:");
for (i = 0;i < a;i ++)
{
printf("第%d名同学\n",i + 1);
for (j = 0;j < b;j ++)
{
if (j == 0)
{
printf("语文:");
scanf("%d",&grade[i][j]);
} else{
printf("数学:");
scanf("%d",&grade[i][j]);
}
}
}
for (i = 0;i < b; i ++)
{
if (i == 0)
{
int sum = 0;
printf("语文的");
for (j = 0;j < a;j ++)
{
sum += grade[j][i];
}
printf("总分为%d,",sum);
printf("平均分为%.2f",(double)sum / a);
putchar('\n');
} else{
int sum= 0;
printf("数学的");
for (j = 0;j < a;j ++)
{
sum += grade[j][i];
}
printf("总分为%d,",sum);
printf("平均分为%.2f",(double)sum / a);
putchar('\n');
}
}
putchar('\n');
for (i = 0;i < a; i ++)
{
printf("第%d名同学的",i + 1);
int sum = 0;
for (j = 0;j < b;j ++)
{
sum += grade[i][j];
}
printf("总分为%d",sum);
printf(",平均分为%.2f",(double)sum / b);
putchar('\n');
}
return 0;
}
5-12
#include
int main()
{
int i, j;
int tensu1[4][3] = {
{
91, 63, 78}, {
67, 72, 46}, {
89, 34, 53}, {
32, 54, 34} };
int tensu2[4][3] = {
{
97, 67, 82}, {
73, 43, 46}, {
97, 56, 21}, {
85, 46, 35} };
int tensu[3][4][3];
int sum[4][3];
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
sum[i][j] = tensu1[i][j] + tensu2[i][j];
}
}
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
tensu[0][i][j] = tensu1[i][j];
}
}
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
tensu[1][i][j] = tensu2[i][j];
}
}
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
tensu[2][i][j] = sum[i][j];
}
}
puts("第一次考试分数:");
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
printf("%4d",tensu[0][i][j]);
}
putchar('\n');
}
puts("第二次考试分数:");
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
printf("%4d",tensu[1][i][j]);
}
putchar('\n');
}
puts("总分:");
for (i = 0;i < 4;i ++)
{
for (j = 0;j < 3;j ++)
{
printf("%4d",tensu[2][i][j]);
}
putchar('\n');
}
return 0;
}