当前位置:网站首页>C语言 2:求三数字最大值,求三数字中间值,编写程序步骤
C语言 2:求三数字最大值,求三数字中间值,编写程序步骤
2022-07-27 10:20:00 【何小柒(qi)~】
1. 例题1
输入三个数字,求其中最大的数字
1.1 三目运算符
#include<stdio.h>
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("max %d \n", max);
return 0;
}
1.2 if语句的嵌套
#include<stdio.h>
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
max = a;
}
else if (b > a && b > c)
{
max = b;
}
else
{
max = c;
}
printf("max %d \n", max);
return 0;
}
1.3 代码复用程度最高(传值赋值)
#include<stdio.h>
int Max_Int(int a, int b)
{
int c = a > b ? a : b;
return c;
}
int main()
{
int a = 0, b = 0, c = 0;//初始化
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = Max_Int(a, Max_Int(b, c));//传值调用,实参赋值给形参
printf("max %d \n", max);
return 0;
}
//代码复用程度最高,即为最佳执行方法
2. 例题2
输入三个数字,求输出中间值。
算法实现:冒泡排序,输入三个值,先将三个值进行排序,在输出中间值,即为所求数字。
2.1 错误示例
代码冗余,未注意代码书写规范
#include<stdio.h>
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
mid = b;
printf("mid=%d \n", mid);
return 0;
}
2.2 方法一
主函数与其他函数分开,代码算法实现清晰可见,代码书写规范清晰良好
#include<stdio.h>
int Mid_Int(int a, int b, int c)
{
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
return b;
}
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
mid = Mid_Int(a, b, c);
printf("mid=%d \n", mid);
return 0;
}
2.3 方法2
形参定义为指针类型,传递地址(交换函数)
#include<stdio.h>
void Swap_Int(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
int main()
{
int x = 10, y = 20;
Swap_Int(x, y);
printf("x=%d,y=%d \n", x, y);
return 0;
}
3. 编写程序的步骤以及过程(适合初学者)
(1)需求:提出自己对于代码的需求,即提出问题
(2)分析:对提出的问题需求进行分析
(3)设计:即对代码的算法进行设计
(4)实施:程序代码的实施,即编码的实现
(5)测试:对自己编写代码的代码模块进行测试
边栏推荐
- Eslint's error message module error (from./node_modules/ [email protected] @Eslint loader / index. JS)
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- Open source project - taier1.2 release, new workflow, tenant binding simplification and other functions
- WEB服务如何平滑的上下线
- Des/3des/aes differences
- Tcp/ip protocol
- MySQL master-slave architecture, read-write separation, and high availability architecture
- 【Liunx】安装MySQL
- 多点双向重发布和路由策略
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
猜你喜欢

Oracle resizing data files

Oracle view hard parsing

Data types and variables

Custom page 01 of JSP custom tag

Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process

Free DIY trip

MySQL数据表的高级操作

Samba server

Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated

【Flink】Flink进行Standalone模式的集群搭建
随机推荐
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
Oracle view hard parsing
家庭琐事问题
ASP. Net core dependency injection journey: 1. Theoretical concepts
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
Ubuntu and MySQL quick start tutorial
[Flink] Flink builds clusters in standalone mode
Detailed analysis of graphs of echats diagram les miserables (chord diagram)
数据类型与变量
Project team summer vacation summary 01
Different binary conversion of MATLAB
es6的foreach与some的循环遍历
Distributed block device replication: client
Samba server
OpenAtom OpenHarmony分论坛,今天14:00见!附大事记精彩发布
【Flink】Flink进行Standalone模式的集群搭建
The core concept and fast practice of shardingsphere
Two architectures of ETL (ETL architecture and ELT Architecture)
MySQL master-slave architecture, read-write separation, and high availability architecture