当前位置:首页 » 编程语言 » 给三个数字排序c语言程序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

给三个数字排序c语言程序

发布时间: 2022-04-25 03:02:32

A. c语言中 怎么对任意三个整数排序(从小到大)

#include
<stdio.h>
int
main()
{
int
t,a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a<b)
{
t=a,a=b,b=t;
}
if(a<c)
{
t=a,a=c,c=t;
}
if(b<c)
{
t=b,
b=c,
c=t;
}
printf("%d
%d
%d\n",c,b,a);
return
0;
}
原理就是运用冒泡算法,把最大的数浮在最上面,而小的数就下沉,最后就输出。

B. 编程C语言:三个数从小到大排序

#include<stdio.h>
intmain()
{
inti,j,m;
doubletemp;
doublea[3];
for(m=0;m<3;m++)//输入三个数
{
scanf("%lf",&a[m]);
}
for(i=0;i<3;i++)//完成排序
{
for(j=i;j<3;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%lf ",a[i]);//打印结果
}
printf(" ");
return0;
}

C. C语言中将三个数字进行排序的几种写法

就三个数字排序,直接两两比较完事了,就一种写法,因为不是大量数据排序,需要考虑时间复杂度和空间复杂度,用不上快速排序,冒泡排序,选择排序这些算法。

D. C语言中要把三个数从大到小排列出来应该怎么编

初学简单版本代码如下:

#include<stdio.h>

int main( )

{

int a, b, c;//定义三个数的变量

int t ;//定义作为交换的变量

scanf ( "%d%d%d" , &a, &b, &c ) ; //取值

if ( a < b )

{t = a; a = b; b = t ;};//如果a,b,进行交换,反之不动

if ( a < c )

{t = a; a = c; c = t ;};//同上

if ( b < c )

{t = b; b = c; c = t ;};

printf( "%-5d%-5d%-5d " , a, b, c);//输出

}

(4)给三个数字排序c语言程序扩展阅读:

C语言中其他多个数排序的方法:

1、冒泡排序法

#include <stdio.h>

#define SIZE 8

void bubble_sort(int a[], int n);

void bubble_sort(int a[], int n)

{

int i, j, temp;

for (j = 0; j < n - 1; j++)

for (i = 0; i < n - 1 - j; i++)

{

if(a[i] > a[i + 1])

{

temp = a[i];

a[i] = a[i + 1];

a[i + 1] = temp;

} } }

int main()

{

int number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12};

int i;

bubble_sort(number, SIZE);

for (i = 0; i < SIZE; i++)

{

printf("%d ", number[i]);

}

}

2、选择排序

#include<stdio.h>

void main()//主函数

{

int a[10];

int i,j,w;

printf("请输入10个数字: ");

for(i=0;i<10;i++)

scanf("%d",&a[i]);

for(i=0;i<10;i++)

{

for(j=i+1;j<10;j++)

if(a[i]<a[j])//进行比较

//比较后进行交换

{

w=a[i];

a[i]=a[j];

a[j]=w;

}

E. C语言中从键盘上输入三个数,将之排序后按由大到小的顺序输出

具体操作方法如下:

#include<stdio.h>

int main(void)

{int a,b,c,t;

printf("请输入三个数");

scanf("%d%d%d",&a,&b,&c);

if(a<b)

{t=a;a=b;b=t;}

if(b<c)

{t=b;b=c;c=t;}

if(a<b)

{t=a;a=b,b=t;}

printf("从大到小:%d %d %d",a,b,c);

system("pause");

return 0;}

注意:中间的t=a;a=b;b=t,就是交换a和b的位置,总是把大的换到前面来。

(5)给三个数字排序c语言程序扩展阅读:

c语言任意输入5个数,并按从大到小顺序输出的方法如下:

#include <stdio.h>

#include <stdlib.h>

int main()

{int a[5];

int i,j,k;

for(i=0;i<=4;i++)

{scanf("%d",&a[i]);}

for(i=0;i<=4;i++){

for(j=0;j<=4;j++)

{if(a[i]>a[j])

{k=a[i]; a[i]=a[j]; a[j]=k;}}}

printf("排序结果是: ");

for(i=0;i<=4;i++)

{ printf("%d ",a[i]);

}return 0;

F. C语言如何从键盘输入任意3个数,按从小到大的顺序输出

代码1.

//输入3个数,要求按从小到大顺序输出

#include<stdio.h>

intmain()
{
inta,b,c,t;
printf("请输入三个数:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
t=a;
a=b;
b=t;
}
if(a>c)
{
t=a;
a=c;
c=t;
}
if(b>c)
{
t=b;
b=c;
c=t;
}
printf("从小到大的顺序是:%d%d%d ",a,b,c);
return0;
}

代码2.

输入3个字符串,按从小到大顺序输出。//先用程序对三个数进行从小到大排序,然后修改程序
#include<stdio.h>
#include<string.h>
intmain()
{voidswap(char*pt1,char*pt2);
chara[20],b[20],c[20];
char*p1,*p2,*p3;
printf("请输入三个字符串:");
gets(a);
gets(b);
gets(c);
//或用scanf("%s,%s,%s",a,b,c);
p1=&a[0];p2=&b[0];p3=&c[0];//三个指针分别指向三个字符数组
if(strcmp(*p1,*p2)>0)swap(p1,p2);//if(strcmp(a,b)>0)swap(a,b);//比较两个字符串的大小,为什么用前一句的时候会出现警告呢

if(strcmp(a,c)>0)swap(a,c);//if(strcmp(*p1,*p3)>0)swap(*p1,*p3);
if(strcmp(b,c)>0)swap(b,c);//if(strcmp(*p2,*p3)>0)swap(*p2,*p3);
printf("由小到大排列:%s %s %s ",a,b,c);
return0;
}

voidswap(char*pt1,char*pt2)
{chart[20];
strcpy(t,pt1);
strcpy(pt1,pt2);
strcpy(pt2,t);
//t=*pt1;*pt1=*pt2;*pt2=t;
}

代码3.

#include<stdio.h>
#include<string.h>
#defineSIZE3
#defineLEN50


intmain(void)
{
charstr[SIZE][LEN];
char(*pst)[LEN]=str;
chartemp[LEN];
inti,j;

printf("Pleaseenter3string. ");

for(i=0;i<SIZE;i++)
{
fgets(*(pst+i),LEN,stdin);
}
printf("Beforsort: ");

for(i=0;i<SIZE;i++)
{
fputs(*(pst+i),stdout);

}
for(i=0;i<SIZE-1;i++)
for(j=i+1;j<SIZE;j++)
{

if(strcmp(*(pst+i),*(pst+j))==1)
{
strcpy(temp,*(pst+i));
strcpy(*(pst+i),*(pst+j));
strcpy(*(pst+j),temp);
}

}
printf("Aftersort: ");
for(i=0;i<SIZE;i++)
{
fputs(*(pst+i),stdout);
}

}

G. 用C语言将3个数排序~

scanf("%d,%d,%d",&a,&b,&c);与请输入三个数a,b,c:3 4 5
这两句写错了,你看看scanf()函数怎么用。
你可以改成:
scanf("%d%d%d",&a,&b,&c);然后输入 :3 4 5
或者不改scanf("%d,%d,%d",&a,&b,&c); 输入改成: 3,4,5

H. C语言中三个数排序

用C语言编写通过if将3个数排序:

#include<stdio.h>/*函数头:输入输出头文件*/

void main()/*空类型:主函数*/

{

inta,b,c,t;/*定义变量的数据类型为整型*/

printf("输入3个数,中间用空格隔开:");/*输出文字提示*/

scanf("%d%d%d",&a,&b,&c);/*输入3个数字*/

if(a<b)/*判断a是否小于b*/

{t=a;a=b;b=t;}/*是,则a、b的值互换*/

if(a<c)/*判断a是否小于c*/

{t=a;a=c;c=t;}/*是,则a、c的值互换*/

if(b<c)/*判断b是否小于c*/

{t=b;b=c;c=t;}/*是,则b、c的值互换*/

printf("从小到大:%d,%d,%d ",c,b,a);/*输出从小到大排列的数*/

printf("从大到小:%d,%d,%d ",a,b,c);/*输出从大到小排列的数*/

}

(8)给三个数字排序c语言程序扩展阅读

输入三个数,比较其大小,并从大到小输出。

#include<stdio.h>

int main(){

inta,b,c;

scanf("%d%d%d",&a,&b,&c);

if(a<b){

intflag=a;

a=b;

b=flag;

}

if(a<c){

intflag=a;

a=c;

c=flag;

}

if(b<c){

intflag=b;

b=c;

c=flag;

}

printf("%d%d%d",a,b,c);

}

I. C语言 三个数排序

两种方法
简单方法
#include<stdio.h>
int
main(void)
{
int
a,
b,
c,
t;
printf("软件的功能是为3个数字排序,从大到小");
printf("请输入三个数字,以空格相间隔:\n");
scanf("%d
%d
%d",
&a,
&b,
&c);
if
(a
<
b)
{
t
=
a;
a
=
b;
b
=
t;
}
if
(a
<
c)
{
t
=
a;
a
=
c;
c
=
t;
}
if
(b
<
c)
{
t
=
b;
b
=
c;
c
=
t;
}
printf("a=%d
b=%d
c=%d",
a,
b,
c);
getchar();
getchar();
return
0;
}
------------------------------------这是分割线------------------------------------------------
复杂方法
#include<stdio.h>
int
main(void)
{
int
x,
y,
z;
printf("此程序为你输入的三个数值自动排序,用a表示最大,b为中间,c为最小\n");
printf("请输入三个不同整数(以空格键相隔):\n");
scanf_s("%d
%d
%d",&x,&y,&z);
//
printf("x=%d
y=%d
z=%d\n",x,y,z);
if
(x
>
y
&&
y
>z)
printf("a=%d,b=%d,c=%d\n",x,y,z);
else
if
(x
>
z
&&
z
>
y)
printf("a=%d,b=%d,c=%d\n",x,z,y);
else
if
(y
>
x
&&
x
>
z)
printf("a=%d,b=%d,c=%d\n",y,x,z);
else
if
(y
>
z
&&
z
>
x)
printf("a=%d,b=%d,c=%d\n",y,z,x);
else
if
(z
>
x
&&
x
>
y)
printf("a=%d,b=%d,c=%d\n",z,x,y);
else
if
(z
>
y
&&
y
>
x)
printf("a=%d,b=%d,c=%d\n",z,y,x);
else
printf("有相同数值无法比较大小,请重新输入不同数值\n");
getchar();
getchar();
return
0;
}

J. 用C语言比较三个数的大小,并按照从大到小排序

#include <stdio.h>
#include <math.h>
int main(void)
{

int a,b,c;
printf("请输入任意三个整数a,b,c:");
scanf("%d%d%d",&a,&b,&c);
int s;
if(a>b)s=a,a=b,b=s;//比较a,b大小,通过赋值,使得a<b 。此语句目的:a<b
if(a>c)s=a,a=c,c=s;//进一步比较a,c大小,通过赋值得出最小值,并赋给a; 此语句目的:a<c
if(b>c)s=b,b=c,c=s;//最后比较出b,c中的较大值。
printf("从大到小的顺序依次是:%d%d%d",c,b,a);
return 0;
}