目录
题目
求2个或3个正整数中的最大值,用带有默认参数的函数实现。
源码
#include<iostream>
using namespace std;
int main()
{
void max(int, int, int z=0);
int a, b, c, i;
cout << "请输入要比较几个正整数(2个或3个)" << endl;
cin >> i;
cout << "请输入要比较的正整数,中间用空格隔开" << endl;
if(i=2)
{
cin >> a >> b;
max(a, b);
}
else if(i=3)
{
cin >> a >> b >> c;
max(a, b, c);
}
else
cout << "需要比较的正整数个数错误" << endl;
return 0;
}
void max(int x,int y, int z)
{
int max;
max = x > y ? x : y;
max = max > z ? max : z;
cout << "正整数的最大值为:" << max << endl;
return ;
}