当前位置:网站首页>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)测试:对自己编写代码的代码模块进行测试
边栏推荐
- gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
- warning: remote HEAD refers to nonexistent ref, unable to checkout报错信息
- Document intelligent multimodal pre training model layoutlmv3: both versatility and superiority
- Distributed block device replication: client
- 文档智能多模态预训练模型LayoutLMv3:兼具通用性与优越性
- FTP server
- 异构计算技术分析
- The permission problem of Oracle operating openldap
- It is thought-provoking: is syntax really important? Qiu Xipeng group proposed a powerful baseline for aspect based emotional analysis
- Error in nodejs: getaddrinfo enotfound localhost
猜你喜欢

ECCV 2022 | 同时完成四项跟踪任务!Unicorn: 迈向目标跟踪的大统一

Your appearance is amazing! Two JSON visualization tools are recommended for use with swagger. It's really fragrant

Warning: remote head references to nonexistent ref, unable to checkout error messages

分享机器学习笔记(PDF版)+实战项目(数据集+代码)

服务器访问速度

Free DIY trip

ctf (hardrce)

Set up Samba service

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

warning package. Json: no license field error
随机推荐
让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
Different binary conversion of MATLAB
[Linux] install redis
MySQL must know and know!!! Reading this article is enough!!!
【Flutter】SharedPreferences使用
Tensorflow notes - basic functions and concepts
Multipoint bidirectional republication and routing strategy
Mysql死锁、悲观锁、乐观锁
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
家庭琐事问题
Metaspolit
PHP generates text and image watermarks
Customized modification based on jira7.9.2
【Flink】Flink进行Standalone模式的集群搭建
一起学习C语言:结构体(二)
FTP server
异构计算技术分析
Sound processing - Mel frequency cepstrum coefficient (MFCC)
Voice data acquisition - real time voice data visualization
JSP自定义标签之自定义分页01