按顺序先后比较四个数的大小然后找出最大值即可实现功能。
范例:
#include<stdio.h>void main()
{
int a, b, c, d, tem
printf("input four numbers:\n")
scanf("%d,%d,%d,%d",&a,&b,&c,&d)
if(a>b)
{tem=aa=bb=tem}
if(a>c)
{tem=aa=cc=tem}
if(a>d)
{tem=aa=dd=tem}
if(b>c)
{tem=bb=cc=tem}
if(b>d)
{tem=bb=dd=tem}
if(c>d)
{tem=cc=dd=tem}
printf("%d,%d,%d,%d",a,b,c,d)
}
Ps:
常用排序方法如图:
上面的范例是以冒泡法编写的程序。
你if后面跟着的cout没有在括号
里面,if为假只是不输出接着的cout,但{
}里面的会继续输出
#include<iostream>
using
namespace
std
int
main(void)
{
int
a,b,c,max,min
cout<<"请输入要比较的2个数:
"
cin>>a>>b
cout<<"1较大数
2较小数:
"
cin>>c
if
(c==1)
{
if
(a>b)
{
max=a
}
else
max=b
cout<<"所求的为较大数="<<max<<endl
}
if
(c==2)
{
if
(a<b)
{
min=a
}
else
min=b
cout<<"所求的为较小数="<<min<<endl
}
return
0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)