当前位置:网站首页>【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;
}
总结
以上就是该题目三种解法,博主比较钟爱第三种,因为代码简洁,目的性也强,就是要理解三目操作符的用法即可,以后也会更新更多常见题目的解法,尽量做到一题多解,开阔思路,找到最优解,最后,喜欢的话,希望家人们给个一键三连多多支持,博主也多加努力!!
+
关注
+
收藏嗷!!
边栏推荐
- 365天挑战LeetCode1000题——Day 046 生成每种字符都是奇数个的字符串 + 两数相加 + 有效的括号
- C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
- 二分法中等 LeetCode6133. 分组的最大数量
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.5 ANSI C Today
- Pytest: begin to use
- Based on php hotel online reservation management system acquisition (php graduation project)
- Small program -- subcontracting
- 基于php在线音乐网站管理系统获取(php毕业设计)
- 基于php影视资讯网站管理系统获取(php毕业设计)
猜你喜欢

scikit-learn no moudule named six

NFT的10种实际用途(NFT系统开发)

ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!

property语法

LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟

Homework 8.1 Orphans and Zombies

小程序--分包

Port protocol for WEB penetration

2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型

groupByKey和reduceBykey的区别
随机推荐
C语言_枚举类型介绍
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.4 K&R C
上传markdown文档到博客园
Based on php online music website management system acquisition (php graduation design)
How to encapsulate the cookie/localStorage sessionStorage hook?
Based on php Xiangxi tourism website management system acquisition (php graduation design)
Dichotomy Medium LeetCode6133. Maximum Number of Groups
Transformer学习
C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
Image fusion GANMcC study notes
AIDL通信
方舟:生存进化官服和私服区别
空间数据库开源路,超图+openGauss风起禹贡
Based on php online learning platform management system acquisition (php graduation design)
基于php在线音乐网站管理系统获取(php毕业设计)
方舟生存进化是什么游戏?好不好玩
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
Day33 LeetCode
51.【结构体初始化的两种方法】
10 Practical Uses of NFTs (NFT System Development)