当前位置:网站首页>Sort the three integers from large to small (introduce various methods in detail)
Sort the three integers from large to small (introduce various methods in detail)
2022-07-27 02:25:00 【Guangguangzi】
Example : Enter three integers , Show in descending order
Input :4 3 5
Output :5 4 3
(1) The easiest way ( Compare the two )
The principle is : Mathematical ranking ( The question of shaking hands in pairs ,n Individuals have (n-1+n-2+....+1) Medium method )

#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
int c = 0;
int tmp = 0; // As a medium of exchange
printf(" Please enter three numbers :\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(" Output the three numbers after sorting :\n");
printf("%d %d %d", a, b, c);
return 0;
}(2) The method of using functions ( Principle Comparison Exchange cmp, Compare the three numbers )
#include<stdio.h>
void cmp(int* _a, int* _b, int* _c)// The corresponding parameter in the function is the formal parameter of the main function argument , A formal parameter is a temporary copy of an argument
{
int tmp = 0;
if (*_a < *_b) //int* Is the type of pointer variable amount to int yes Type of integer
{
tmp = *_a; //_a =&a *_a = a *&a=a * The function here is that the dereference operation can find the arguments
*_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(" Please enter three numbers :\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b, &c);
printf(" Output the three numbers after sorting :\n");
printf("%d %d %d", a, b, c);
return 0;
}(3) Function method ( Compare the two numbers )
#include<stdio.h>
void cmp(int* px, int* py) //px,py Is an address pointer that stores two numbers , It's a pointer variable , The pointer variable is the address
{
int z = 0; //z For temporary storage of data , The medium of exchange
if (*px < *py) //*px Pointer operation *px=a(a The number stored for the address )
{
z = *px;
*px = *py;
*py = z;
}
}
int main()
{
int a = 0;
int b = 0;
int c = 0;
printf(" Please enter three numbers :\n");
scanf("%d %d %d", &a, &b, &c);
cmp(&a, &b);
cmp(&a, &c);
cmp(&b, &c);
printf(" Output the three numbers after sorting :\n");
printf("%d %d %d", a, b, c);
return 0;
}(4) Use bubble sort ( Yes, but not necessarily )
#include<stdio.h>
int main()
{
int a[3] = { 0 };// Set the array to store three numbers , Plan to complete the order of storage from large to small
int i = 0;
int j = 0;
printf(" Please enter three numbers :\n");
for (i = 0; i < 3; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < 2; i++) //3 A digital , The first number needs to be compared with the remaining two numbers , Just do it twice
{
int tmp = 0;
for (j = 0; j < 2 - i; j++)// It has been compared before , Subtract the number of previous comparisons
{
if (a[j] < a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
printf(" Output the three numbers after sorting :\n");
for (i = 0; i < 3; i++)
{
printf("%d ", a[i]);
}
return 0;
}边栏推荐
猜你喜欢

HCIA (network elementary comprehensive experimental exercise)

Esp8266wi fi data communication

求解100~200之间的素数

HCIP-第四天-OSPF路由协议

记录第N次SQL异常

HandsomeForum学习论坛

OSPF configuration in mGRE environment and LSA optimization - reduce the amount of LSA updates (summary, special areas)

【洋哥带你玩转线性表(三)——双向链表】

今天浅讲一下转义字符【萌新版】

记录HandsomeBlog的star用户
随机推荐
HCIP-第四天-OSPF路由协议
Golang中的错误处理
【C语言】strlen与sizeof相关区分
Hcip OSPF knowledge summary
怎么判断一个数是奇数还是偶数?
Timer interrupt experiment
Nb-iot access to cloud platform
【C语言】阶乘实现
C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
HCIP oSPF综合实验
OSPF basic experimental configuration
Influence of pre frequency division value and automatic reload value on interrupt frequency
多点双向重发布和路由策略-拓扑实验
【降维打击,带你深度学习CPU(上)】
OSPF路由信息协议-拓扑实验
MySQL course 1. simple command line -- simple record welcome to supplement and correct errors
js中的数组方法和循环
Wechat applet: user wechat login process (attached: flow chart + source code)
Esp8266wi fi data communication
(史上最详细)Codeforces Round #805 (Div. 3)E. Split Into Two Sets