① 急求謝啦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;
}
}