任意输入三个数并分别保存在x1,x2,x3三个变量中,按从大到小的顺序重排x1,x2,x3,使x1最大,x3最小
写一个简单的程序
- 提问者网友:送舟行
- 2021-04-20 06:28
- 五星知识达人网友:枭雄戏美人
- 2021-04-20 06:50
C语言写的,用的整数,要是要求用实数把int改成double就行了
void main()
{
int x1, x2,x3;
printf("Input 3 numbers:");
scanf("%d",&x1);
scanf("%d",&x2);
scanf("%d",&x3);
int t
if(x1<x2)
{
t=x1;
x1=x2;
x2=t;
}
if(x1<x3)
{
t=x1;
x1=x3;
x3=t;
}
if(x2<x3)
{
t=x2;
x2=x3;
x3=t;
}
printf("%d,%d,%d",&x1,x2,x3);
}
- 1楼网友:长青诗
- 2021-04-20 09:32
写个脚本的很简单,
var max=Math.max(Math.max(x1,x2),x3);
var min=Math.min(Math.min(x1,x2),x3);
var sec;
if(x1!=max && x1!=min){
sec=x1
}
if(x2!=max && x2!=min){
sec=x2
}
if(x3!=max && x3!=min){
sec=x3
}
x1=max;x2=sec;x3=min;
ok!大概就是这个样子了,要是用数组冒泡法会更简单,懒得写了
- 2楼网友:一叶十三刺
- 2021-04-20 08:07
我只会free pascal语言:
var x1,x2,x3:integer;
begin
readln(x1,x2,x3);
if (x1<x2) then begin x2:=x2+x1; x1:=x2-x1; x2:=x2-x1; end;
if (x1<x3) then begin x3:=x3+x1; x1:=x3-x1; x3:=x3-x1; end;
if (x3>x2) then begin x3:=x3+x2; x2:=x3-x2; x3:=x3-x2; end;
writeln(x1,' ',x3);
end.