三个整数的数值比较

三个整数求最大值最小值和中间值


#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;
}
}
}
}
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值