当前位置:网站首页>【C语言实现】求两个整数的较大值
【C语言实现】求两个整数的较大值
2022-08-01 21:34:00 【万羽西】
方法一
通过 if else 来判断大小,分别打印。
#include <stdio.h>
int main()
{
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
if (x > y)
{
printf("max = %d\n", x);
}
else
{
printf("max = %d\n", y);
}
return 0;
}
方法二
利用函数求出最大值
#include <stdio.h>
int Max(int x, int y)
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
int main()
{
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
int max = Max(x, y);
printf("max = %d\n", max);
return 0;
}
方法三
利用三目操作符(条件操作符)比较
#include <stdio.h>
int main()
{
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
int max = x > y ? x : y;
printf("max = %d\n", max);
return 0;
}
总结
以上就是该题目三种解法,博主比较钟爱第三种,因为代码简洁,目的性也强,就是要理解三目操作符的用法即可,以后也会更新更多常见题目的解法,尽量做到一题多解,开阔思路,找到最优解,最后,喜欢的话,希望家人们给个一键三连多多支持,博主也多加努力!!
+
关注
+
收藏嗷!!
边栏推荐
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.4 K&R C
- 空间数据库开源路,超图+openGauss风起禹贡
- 小程序--分包
- The Microsoft campus ambassador to shout you to autumn recruit!
- 软考 ----- UML设计与分析(上)
- The difference between groupByKey and reduceBykey
- Appendix A printf, varargs and stdarg A.1 printf family of functions
- Based on php online music website management system acquisition (php graduation design)
- Popular explanation: what is a clinical prediction model
- render-props和高阶组件
猜你喜欢

The difference between groupByKey and reduceBykey

ARFoundation Getting Started Tutorial U2-AR Scene Screenshot Screenshot

An online JVM FullGC made it impossible to sleep all night and completely crashed~

MySQL related knowledge

How to choose Visibility, Display, and Opacity when interacting or animating

365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号

Based on php online music website management system acquisition (php graduation design)

AI应用第一课:支付宝刷脸登录

作业8.1 孤儿进程与僵尸进程

TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
随机推荐
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.5 ANSI C Today
基于php在线考试管理系统获取(php毕业设计)
基于php影视资讯网站管理系统获取(php毕业设计)
关于npm的那些事儿
Pytest: begin to use
Mini Program--Independent Subcontracting & Subcontracting Pre-download
Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
ARFoundation入门教程U2-AR场景截图截屏
(七)《数电》——CMOS与TTL门电路
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
Shell programming conditional statement
HCIP---多生成树协议相关知识点
基于php旅游网站管理系统获取(php毕业设计)
Realize the superposition display analysis of DWG drawing with CAD in Cesium
方舟:生存进化PVE模式和PVP模式
Shell编程条件语句
The Microsoft campus ambassador to shout you to autumn recruit!
Scala练习题+答案
C Pitfalls and Defects Chapter 5 Library Functions 5.5 Library Function Signal
Anacoda的用途