當前位置:首頁 » 編程語言 » 給三個數字排序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;
}