当前位置:网站首页>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)测试:对自己编写代码的代码模块进行测试
边栏推荐
- WebRTC实现简单音视频通话功能
- Metasploit Eternal Blue attack
- Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
- Warning: remote head references to nonexistent ref, unable to checkout error messages
- 【Liunx】安装Redis
- 华硕无双,这可能是屏幕最好的平价高刷轻薄笔记本
- JVM -- Analysis of bytecode
- It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
- 基于Spark封装的二次开发工程edata-base,介绍
- 简单几步教您实现为工业树莓派共享网络
猜你喜欢

让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...

flask_restful中的输出域(Resource、fields、marshal、marshal_with)

Mail server

Set up Samba service

It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis

ASP. Net core dependency injection journey: 1. Theoretical concepts

Li Kou brush question 02 (sum of three numbers + sum of maximum subsequence + nearest common ancestor of binary tree)

How to smooth the online and offline of Web Services

warning package. Json: no license field error

Customized modification based on jira7.9.2
随机推荐
Error in nodejs: getaddrinfo enotfound localhost
[intensive reading of thesis]bert
A few simple steps to realize the sharing network for industrial raspberry pie
Basic statement of database operation
Matlab draws the system response under different damping
【Liunx】MariaDB/MySQL定时全量备份脚本及数据恢复
Echats关系图les-miserables的图表详细解析(和弦图)
Key points of ES6 class inheritance
已解决SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated
Matlab- draw date and duration diagram
[brother hero's June training] day 26: check the collection
Multipoint bidirectional republication and routing strategy
【Liunx】安装Redis
数据类型与变量
Redis数据结构分析(二)
It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
MySQL must know and know!!! Reading this article is enough!!!
Program translation and execution, from editing, preprocessing, compilation, assembly, linking to execution
Gamer questions
Local connection to remote server database under Windows platform (I)