您的位置:首页 > 其它

四个整数求最大值问题

2014-05-29 15:49 134 查看
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//求三个数的最大值

/*

#include <iostream>

using namespace std;

int main()

{

int a,b,c;

cout <<"请输入3个整数:"<<endl;

cin>> a>>b>>c;

if(a > b)

{

if(a > c)

cout << a<<endl;

else

cout << c<<endl;

}

else

{

if(b > c)

cout << b<<endl;

else

cout << c<<endl;

}

return 0;

}

*/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//由求三个数的最大值推出求四个数的最大值,该方法容易想到,但是,容易推错,

/*

#include <iostream>

using namespace std;

int main()

{

int a,b,c,d;

cout<< "求四个数的最大值,请输入4个数"<<endl;

cin >>a>>b>>c>>d;

if(a > b)

{

if(a > c)

{

if(a > d)

{

cout << a <<endl;

}

else

cout << d << endl;

}

else

{

if(c > d )

cout << c << endl;

else

cout << d;

}

}

else

{

if(b > c)

{

if(b > d)

cout << b <<endl;

else

cout << d << endl;

}

else

{

if(c > d)

cout << c <<endl;

else

cout << d <<endl;

}

}

return 0;

}

*/

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/* 输出四个整数的最大值*/

//引入变量,逐个比较

/*

#include <iostream>

using namespace std;

int main()

{

int a,b,c,d,max1;

cout <<"请输入四个整数"<<endl;

cin >> a >> b >> c >> d;

max1 = a;

if(max1 < b)

max1 = b;

if(max1 < c)

max1 = c;

if(max1 < d)

max1 =d;

cout <<"最大值是"<< max1 <<endl;

return 0;

}

*/

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//稍作扩展,求四个数的最大,最小值

#include <iostream>

using namespace std;

int main()

{

int a,b,c,d,max,min;

cout <<"请输入4个整数"<<endl;

cin >> a >> b >> c >> d;

max = a;

if(max < b)

max = b;

if(max < c)

max = c;

if(max < d)

max = d;

min = a;

if(min > b)

min = b;

if(min > c)

min = c;

if(min > d)

min = d;

cout <<"最大值为"<< max <<endl;

cout <<"最小值为"<< min <<endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐