当前位置:网站首页>三个整数从大到小排序(详细介绍多种方法)
三个整数从大到小排序(详细介绍多种方法)
2022-07-26 22:49:00 【光光光仔】
例题:输入三个整数,按照从大到小的顺序表示出
输入:4 3 5
输出:5 4 3
(1)最简单的方法(两两进行比较)
原理是:数学的排序(两两握手的问题,n个人有(n-1+n-2+....+1)中方法)

#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int tmp = 0; //作为交换的媒介
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
if (a < b)
{
tmp = a;
a = b;
b = tmp;
}
if (a < c)
{
tmp = a;
a = c;
c = tmp;
}
if (b < c)
{
tmp = b;
b = c;
c = tmp;
}
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(2)使用函数的方法(原理比较交换cmp,三个数字比较)
#include<stdio.h>
void cmp(int* _a, int* _b, int* _c)//函数里对应参数为主函数实参的形参,形参是实参的临时拷贝
{
int tmp = 0;
if (*_a < *_b) //int* 是指针变量的类型 相当于 int 是 整数的类型
{
tmp = *_a; //_a =&a *_a = a *&a=a *这里的作用是解引用操作可以找到实参
*_a = *_b;
*_b = tmp;
}
if (*_a < *_c)
{
tmp = *_a;
*_a = *_c;
*_c = tmp;
}
if (*_b < *_c)
{
tmp = *_b;
*_b = *_c;
*_c = tmp;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b, &c);
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(3)函数方法(两个数字之间比较)
#include<stdio.h>
void cmp(int* px, int* py) //px,py是存储两个数的地址指针,是指针变量,指针变量就是地址
{
int z = 0; //z为暂时存储数据的场所,交换媒介
if (*px < *py) //*px 解指针操作 *px=a(a为地址存储的数字)
{
z = *px;
*px = *py;
*py = z;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf("请输入三个数字:\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b);
cmp(&a, &c);
cmp(&b, &c);
printf("输出排序后的三个数字:\n");
printf("%d %d %d", a, b, c);
return 0;
}(4)运用冒泡排序(可以但没必要)
#include<stdio.h>
int main()
{
int a[3] = { 0 };//设置数组存储三个数字,打算完成从大到小的顺序存放
int i = 0;
int j = 0;
printf("请输入三个数字:\n");
for (i = 0; i < 3; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < 2; i++) //3个数字,第一个数字需要和剩下两个数字比较,只需进行两次
{
int tmp = 0;
for (j = 0; j < 2 - i; j++)//前面已经比较了,减去前面比较的次数
{
if (a[j] < a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
printf("输出排序后的三个数字:\n");
for (i = 0; i < 3; i++)
{
printf("%d ", a[i]);
}
return 0;
}边栏推荐
- The latest C language introduction and advanced - the most complete and detailed C language tutorial in history!! Section 1 - overview of C language
- Nb-iot networking communication
- 微信小程序:用户微信登录流程(附:流程图+源码)
- OSPF protocol knowledge summary
- ensp中的简单静态路由
- HCIA静态路由综合实验
- HCIA (network elementary comprehensive experimental exercise)
- Nat网络地址转换实验
- WAN technology experiment
- Static routing default routing VLAN experiment
猜你喜欢

C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释

Explain exi interrupt through the counting experiment of infrared sensor

广域网技术实验

Tim output comparison - PWM

Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan

动态路由ofps协议配置

静态路由基础配置(IP地址的规划、静态路由的配置),实现全网可达。

Introduction to STM32 lesson 1

RS-485 bus communication application

NAT network address translation experiment
随机推荐
[Database Course Design] SQLSERVER database course design (student dormitory management), course design report + source code + database diagram
OSPF protocol overview and basic concepts
C language implementation of the small game [sanziqi] Notes detailed logic clear, come and have a look!!
Dynamic routing rip protocol experiment
Lvs+keepalived project practice
OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)
First acquaintance with C language (1)
Dynamic routing ofps protocol configuration
记录HandsomeBlog的star用户
(CF1691D) Max GEQ Sum
静态路由缺省路由vlan实验
C语言——赋值运算符、复合的赋值运算符、自增自减运算符、逗号运算符、条件运算符、goto语句、注释
HCIA Basics (1)
First knowledge of Web Design
C语言——数据类型、基本数据类型的取值范围
Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
NB-IOT联网通信
Ospf基础配置应用( 综合实验: 干涉选举 缺省路由 区域汇总 认证--接口认证)
RIP V2 的简单应用(v2的配置、宣告、手工汇总、RIPV2的认证、沉默接口、加快收敛)
二层封装技术(HDLC、PPP--PAP\CHAP、GRE)实验练习