当前位置:网站首页>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语言最基本的使用,以及代码的多种形态。
当然,比大小也绝不仅这三种形式的写法,想告诉大家的是什么呢,代码并不是一成不变的,你可以多种思路多种方法去达到目的,不要仅局限于一种形式,画地为牢。
希望能帮助大家更好的去理解别人写的代码,以及更好的写出代码。
边栏推荐
- In JS, the common writing methods and calling methods of functions - conventional writing, anonymous function writing, taking the method as an object, and adding methods to the object in the construct
- 滑动窗口问题总结
- deeplabcut使用1
- Class and object notes I
- Flink SQL (II) Kafka connector
- Find method of web page parsing by crawler
- Design of vision protector based on 51 single chip microcomputer
- UNET notes
- 转置卷积相关
- 爬虫解析网页的find方法
猜你喜欢

Complete review of parsing web pages

Web middleware log analysis script 2.0 (shell script)

Convolutional neural network -- lenet (pytorch Implementation)

CCPD data set processing (target detection and text recognition)

Configure deeplobcut 1 with your head covered

Deep learning of parameter adjustment skills

LeetCode——哈希表篇

Knowledge distillation -- pytorch implementation

滑动窗口问题总结

Midge paper reading notes
随机推荐
CSDN article syntax rules
Go exceed API source code reading (IV) -- save (), SaveAs (name string)
yolov5在jetson nano上的部署 deepstream
深度学习调参技巧
Shufflenet series (2): explanation of shufflenet V2 theory
MySQL transaction, phantom reading, current reading, snapshot reading related notes
Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
放图仓库-3(功能图像)
爬虫解析网页的 对象.元素名方法
Recent answers - column
在pycharm中部署yolov5报错问题
LeetCode题目——数组篇
Pyautogui usage example
C and pointer Chapter 18 runtime environment 18.7 problems
Double. isNaN(double var)
Fourier analysis (basic introduction)
Iptables prevent nmap scanning and binlog
Alexnet (pytoch Implementation)
Blue Bridge Cup brush question notes (word analysis)
Matlab simulation of inverted pendulum control system based on qlearning reinforcement learning