提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
萌新学习网课的同时,也购买了《C程序设计》谭浩强第五版的教材和习题,所以后续将会做一个系列,持续更新这本书上的萌新的训练答案。
一、第一章
教材课后习题
1.P14 习题6
任务描述:
编写一个C程序,运行时输入a,b,c三个值,输出其中值最大者
输入描述:
40 212 12
输出描述:
212
代码如下(示例)
法一:函数法
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
int compare(int x,int y,int z) {
if (x > y and x > z)
return x;
if (y > x and y > z)
return y;
if (z > x and z > y)
return z;
}
int main()
{
//编写程序:输入a,b,c,输出其中最大值
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int ret = compare(a, b, c);
printf("% d", ret);
return 0;
}
法二:if语句
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (a > b and a > c) {
printf("%d",a) ;
}
else if (b > a and b > c) {
printf("%d", b);
}
else{
printf("%d", c);
}
return 0;
}
学习辅导习题
1.P2 习题5
任务描述:
编写一个C程序,运行时输入以下图形:
输出描述:
*****
*****
*****
*****
代码如下(示例)
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
int main()
{
printf("*****\n");
printf(" *****\n");
printf(" *****\n");
printf(" *****\n");
return 0;
}
2.P2 习题6
任务描述:
编写一个C程序,运行时输入a,b,c三个值,输出其中值最大者。本题在上述已经讲过(萌新自己的写法),这里补充一个新的方法(课本上的代码写法)。
输入描述:
40 212 12
输出描述:
212
代码如下(示例)
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
int main()
{
int a, b, c ,max;
scanf("%d %d %d", &a, &b, &c);
max = a;
if (max < b)
max = b;
if (max < c)
max = c;
printf("%d\n", max);
return 0;
}
问题分析
此方法和上述萌新的if思路大致相同,但是教材中的方法,更加的简洁。萌新是比较每一次a,b,c的大小,而教材则用max,使得减少比较的次数。
总结
萌新在这一篇文章讲述了C程序设计教材和学习辅导的部分习题代码,代码有些是萌新的原创代码,所以可能会存在一些代码复杂的情况,如果有需要进步的地方,欢迎大家来私信我。