当前位置:网站首页>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语言最基本的使用,以及代码的多种形态。
当然,比大小也绝不仅这三种形式的写法,想告诉大家的是什么呢,代码并不是一成不变的,你可以多种思路多种方法去达到目的,不要仅局限于一种形式,画地为牢。
希望能帮助大家更好的去理解别人写的代码,以及更好的写出代码。
边栏推荐
- Design of electronic scale based on 51 single chip microcomputer
- Halloween treatments (drawer principle)
- Machine learning model -- lightgbm
- 9_逻辑回归(Logistic Regression)
- Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection
- 解析网页的完整回顾
- Codeforces D. two divisors (number theory, linear sieve)
- 12_ Decision tree
- 10_ Evaluate classification
- Codeforces E. maximum subsequence value (greed + pigeon nest principle)
猜你喜欢

Convolutional neural network -- lenet (pytorch Implementation)

在pycharm中部署yolov5报错问题

Midge paper reading notes

Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection

Today's 20220719 toss deeplobcut

C and pointer Chapter 18 runtime environment 18.1 judgment of runtime environment

卷积神经网络——LeNet(pytorch实现)

6_梯度下降法(Gradient Descent)

5_线性回归(Linear Regression)

4-4 object lifecycle
随机推荐
三层架构 模拟
100. Same tree
Alexnet (pytoch Implementation)
Mysql8 installation
深度学习调参技巧
JS, one of the methods of object merging Assign (), recursive assignment & method of array merging..., array. Concat (), array. Push. Apply (), array. Push ()
PTA 7-4 small generation (DFS)
Relationship between limit, continuity, partial derivative and total differential of multivariate function (learning notes)
Sliding window problem summary
[PCB open source sharing] stc32g12k128/stc8h8k64u development board
Search engine hijacking of black hat SEO
ResNet论文解读及代码实现(pytorch)
数据库:MySQL基础+CRUD基本操作
Viterbi Viterbi decoding bit error rate simulation, modulation is QPSK, channel is Gaussian white noise
Design of electronic scale based on 51 single chip microcomputer
LeetCode——哈希表篇
Collection of 3D LUT related articles
LeetCode题目——二叉树篇
查看 Anaconda 创建环境的位置
Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection