C++使用指针将3个整数进行比较大小

C++使用指针将3个整数进行比较大小

任务描述

输入3个整数,按由小到大的顺序输出(要求用指针或引用方法处理)。

测试输入:

4 91 51

预期输出:

4 51 91

测试输入:

5 1 151

预期输出:

1 5 151

源代码:

#include <stdio.h>
#include <iostream>
using namespace std;

void mycmp(int x,int y,int z);
int main()
{
    
    // 请在此添加代码
    /********** Begin *********/
	int x,y,z;
	cin>>x>>y>>z;
	mycmp(x,y,z);
    
    /********** End **********/
    return 0;
}
void mycmp(int x,int y,int z){
	int temp;
	if(x>y){
		temp = x;
		x = y;
		y = temp;
	}
	if(x>z){
			temp = x;
			x = z ;
			z = temp;
		}
	if(y>z){
		temp = y;
		y = z;
		z = temp;
	}
	cout<<x<<" "<<y<<" "<<z;
}

猜你喜欢

转载自www.cnblogs.com/lightice/p/12691749.html