三个整数求最大值最小值和中间值
#include<iostream>
using namespace std;
//三个整数中最大值 最小值 中间值的输出
int max(int a, int b, int c) {
int result=a;
if (result<b)
result = b;
if (result < c)
result = c;
return result;
}
int min(int a, int b, int c) {
int result = a;
if (b < result)
result = b;
if (c < result)
c = result;
return result;
}
//求最大值和最小值时 用其中一个整数与另一个整数比较,令result始终等于较大的整数
int mid(int a,int b,int c) {
int result = a;
if (a < b) {
if (b < c)
result = b;
else {
if (a < c)
result = c;
else
result = a;
}
}
else {
if (b < c)
result = b;
else {
if (a > b)
result = b;
else
result = a;
}
}
return result;
}
int mid1(int a, int b, int c) {
int m1 = a, m2; //m1 存放最小的值 m2 中间值
if (b < m1) {
m1 = b;
m2 = a;
}
else
m2 = b;
if (c < m1) {
m1 = c;
m2 = m1;
}
else// 最小值m1 比较m2和c
if(c<m2){
m2 = c;
}
return m2;
}
int main()
{
int a, b, c;
cin >> a >> b >> c;
cout << max(a, b, c) << endl;
cout << min(a, b, c) << endl;
cout << mid(a, b, c) << endl;
cout << mid1(a,b,c) << endl;
system("pause");
return 0;
}
/*求中间值的算法思想
int mid(int a,int b,int c){
int result=a;
if(result<b){
b和c进行比较
if(b<c)
result=b; //b就是中间值
else{
a和c进行比较
if(a<c)
result=c;
else
result=a;
}else{ //a>b
if(b>c)
result=b;
else{
比较a和c;
if(a<c)
result=a;
else result=c;
}
}
}
}
*/