導航:首頁 > 編程語言 > 三數排序怎樣編程

三數排序怎樣編程

發布時間:2023-01-03 10:18:29

① 急求謝啦C語言編程 按從大到小排序三個數

#include <stdio.h>
void main()
{
int a,b,c,t;
printf("輸入三個數:空格隔開"); /*提示輸入3數*/
scanf("%d %d %d",&a, &b, &c);/*由鍵盤輸入*/
printf("排列後:%d %d %d\n",a,b,c);
if(a<b)
{
t=b;
b=a;
a=t;
} /*實現ab互換*/
if(a<c)
{
t=c;
c=a;
a=t;

} /*實現ac互換*/
if(b<c)
{
t=c;
c=b;
b=t;
} /*實現bc互換*/

printf("排列後:%d %d %d \n",a,b,c);

}

java 編程題 對給定的3個數進行排序按從大到小的順序排列輸出

//第一種簡單方法:
public class C123{
public static void main(String args[]){
int a=34,b=62,c=5;,smallest;
sort3(a,b,c);}
static void sort3(){
int temple;
if(x>y){temple=x;x=y;y=temple;}
if(x>z){temple=x;x=z;z=temple;}
if(y>z){temple=y;y=z;z=temple;}
System.out.println("Sorted:"+x+","+y+","+z);
return;
}
}

//第二種簡單方法:
import java.util.*;
class ArraySort{
public static void main(String args[]){
int a[]={,23,64,25};
Arrays.sort(a);
for(i=0;i<a.lengh;i++)
System.out.println(a[i]+" ");
}
}
如果調用方法的話就更好了,你自己琢磨.

③ 如何用函數實現三個數的排序(用的是C語言)

一:全局變數

#include<stdio.h>

inta,b,c;//定義三個全局變數

voidsort()//不需要參數
{
intt;
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;
}
}

intmain()
{
printf("輸入:");
scanf("%d%d%d",&a,&b,&c);
sort();
printf("排序:%d<%d<%d ",a,b,c);
return0;
}

④ 用java編寫程序對三個整數排序

簡單的整數排序,可以用選擇排序、冒泡排序、插入排序。

code demo:

publicclassSortDemo{
publicstaticvoidmain(String[]args){
int[]ary={3,1,5,6,2,6,8,3};
//ary=selectionSort(ary);
ary=insertSort(ary);
//將數組ary連接為一個字元串:Arrays.toString(ary);
//如:{1,2,3}->"[1,2,3]"
Strings=Arrays.toString(ary);
System.out.println(s);
}
/**選擇排序*/
publicstaticint[]selectionSort(int[]ary){
for(inti=0;i<ary.length-1;i++){
for(intj=i+1;j<ary.length;j++){
if(ary[i]>ary[j]){
inttemp=ary[i];
ary[i]=ary[j];
ary[j]=temp;
}
}
}
returnary;
}
/**冒泡排序*/
publicstaticint[]bubleSort(int[]ary){
for(inti=0;i<ary.length-1;i++){
for(intj=0;j<ary.length-(1+i);j++){
if(ary[j]>ary[j+1]){
inttemp=ary[j];
ary[j]=ary[j+1];
ary[j+1]=temp;
}
}
}
returnary;
}
//插入排序
publicstaticint[]insertSort(int[]ary){
//int[]ary={3,1,5,6,2,6,8,3};
for(inti=1;i<ary.length;i++){
inttemp=ary[i];
intj;
for(j=i-1;j>=0&&temp<ary[j];j--){
//if(temp<ary[j]){
ary[j+1]=ary[j];
//}else{
//break;//找到插入位置
//}
}
ary[j+1]=temp;//插入操作
}
returnary;
}
}
閱讀全文

與三數排序怎樣編程相關的資料

熱點內容
肌肉塑造全書pdf下載 瀏覽:796
安卓簡約拼圖用什麼軟體好 瀏覽:289
fx1n加密程序 瀏覽:844
淘客阿里雲伺服器 瀏覽:476
100壓縮打造 瀏覽:422
安卓手機怎麼和蘋果平板傳文件 瀏覽:973
開始選項卡中的頁眉和頁腳命令選項 瀏覽:424
pdf的字體怎麼改 瀏覽:856
python讀寫視頻 瀏覽:88
科魯茲壓縮機軸承 瀏覽:353
word文檔轉換成pdf文件找不到 瀏覽:27
組件注冊命令 瀏覽:760
安卓大屏導航用的是什麼運放 瀏覽:443
myandroidtools的備份 瀏覽:900
python爬蟲天氣預報 瀏覽:761
android70許可權管理 瀏覽:749
魔獸辛迪加是什麼伺服器 瀏覽:472
電腦文件夾排序怎麼自定義排序 瀏覽:41
android70機型 瀏覽:422
阿里程序員地位 瀏覽:183