当前位置:网站首页>c语言 比大小的多种描述,不要只拘泥于一种写法
c语言 比大小的多种描述,不要只拘泥于一种写法
2022-07-26 22:37:00 【TheshyO_o】
目录
前言
c语言入门代码 比大小的多种实现
正如生活中的各种问题,总有各种方法去解决,代码也是一样的
一、为什么要去尝试多种写法
有助于前期思路的打开,更快的去接受并适应代码的世界
还可以帮助大家更好的看懂别人写的代码,看到不一样的代码时可以去理解它而不是排斥
二、实践
1.第一种写法
代码如下:
int main()//比大小 1
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
if (a > b)
printf("%d\n", a);
else
printf("%d\n", b);
return 0;
}直接在main中实现a和b的计算,并且输出较大值
2.第二种写法
代码如下:
int max(int x, int y) //比大小 2
{
if (x > y)
printf("%d\n", x);
else
printf("%d\n", y);
return 0;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
int c = max(a, b);
return 0;
}创建一个max,在max中同时实现比大小以及输出,整个都拿到外面去实现,让主代码更加的简洁
3.第三种写法
int max(int x,int y) //比大小 3
{
if (x > y)
return x;
else
return y;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
int c = max(a, b);
printf("%d\n", c);
return 0;
}在max中计算,从main中输出
总结
以上就是今天要讲的内容,本文仅仅简单介绍了c语言最基本的使用,以及代码的多种形态。
当然,比大小也绝不仅这三种形式的写法,想告诉大家的是什么呢,代码并不是一成不变的,你可以多种思路多种方法去达到目的,不要仅局限于一种形式,画地为牢。
希望能帮助大家更好的去理解别人写的代码,以及更好的写出代码。
边栏推荐
- Complete review of parsing web pages
- Leetcode - linked list
- 机器人学台大林教授课程笔记
- Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection
- 哨兵2号(Sentinel-2)的下载及处理
- Tree and binary tree (learning notes)
- C and pointer Chapter 18 runtime environment 18.7 problems
- 滑动窗口问题总结
- The crawler parses the object of the web page. Element name method
- UNET notes
猜你喜欢
随机推荐
2022_ SummerBlog_ 008
Resolve Microsoft 365 and Visio conflicts
Signal and system impulse response and step response
Leetcode - linked list
Deep learning of parameter adjustment skills
Drawing warehouse-2 (function image)
类与对象笔记一
Deeplabcut uses 1
Codeforces C1. Simple Polygon Embedding
Abstract classes and interfaces (sorting out some knowledge points)
Recent answers - column
C and pointers Chapter 18 runtime environment 18.4 summary
12_ Decision tree
PTA 7-1 play with binary tree
Openharmony quick start
机器人学台大林教授课程笔记
三层架构 模拟
[PCB open source sharing] stc32g12k128/stc8h8k64u development board
"Syntaxerror: future feature annotations is not defined"
8_多项式回归及模型泛化(Polynomial Regression and Model Generalization)









