#include
using namespace std;
void MaxAndMin(int *v,int length,int* max,int* min)
{
*max=*min=v[0];
for(int i=1;i
if(v[i]>*max) *max=v[i];
if(v[i]<*min) *min=v[i];
}
}
void main()
{
int v[10]={1,2,5,45,788,5,8,9,5,46};
int max,min;
MaxAndMin(v,10,&max,&min);
cout<
以上是指针形式。
下面是引用形式,虽然形参表里没有*号,但数组仍是指针传递的。
#include
using namespace std;
void MaxAndMin(int v[],int length,int& max,int& min)
{
max=min=v[0];
for(int i=1;i
if(v[i]>max) max=v[i];
if(v[i]
}
void main()
{
int v[10]={1,2,5,45,788,5,8,9,5,46};
int max,min;
MaxAndMin(v,10,max,min);
cout<
# include
void MaxAndMin1(int n,int a[],int *pMax,int *pMin)
{
*pMax = *pMin =a[0];
for(int i = 1;i
if(*pMax *pMax = a[i];
if(*pMin>a[i])
*pMin = a[i];
}
}
void MaxAndMin2(int n,int a[],int &pMax,int &pMin)
{
pMax = pMin =a[0];
for(int i = 1;i
if(pMax pMax = a[i];
if(pMin>a[i])
pMin = a[i];
}
}
void main()
{
int n=7;
int a[7] = {4,2,3,4,5,6,7};
int *p = new int;
int *q = new int;
MaxAndMin1(n,a,p,q);
cout<<*p<<*q<
int p1 = 0;
int q1 = 0;
MaxAndMin2(n,a,p1,q1);
cout<
#include
#define N // 输入数字个数
int main()
{
int max, min, temp;
printf("输入n个数:");
scanf("%d",&temp);
max=min=temp;
for(int i = 1; i < N; ++i)
{
scanf("%d",&temp);
max=max>temp?max:temp;
min=min
printf("Maximum %d Minumum %d",max,min);
return 0;
}
#include
using namespace std;
void MaxAndMin1(int n,int a[],int *pMax,int *pMin)
{
int i,small,big;
if(n%2==0)
{
if(a[0]>a[1])
{
*pMax=a[0];
*pMin=a[1];
}
else
{
*pMax=a[1];
*pMin=a[0];
}
i=2;
}
else
{
*pMax = *pMin =a[0];
i=1;
}
for(;i
if(a[i]>a[i+1])
{
big=a[i];
small=a[i+1];
}
else
{
big=a[i+1];
small=a[i];
}
if(big>*pMax)
*pMax=big;
if(small<*pMin)
*pMin=small;
}
}
void MaxAndMin2(int n,int a[],int &pMax,int &pMin)
{
int i,small,big;
if(n%2==0)
{
if(a[0]>a[1])
{
pMax=a[0];
pMin=a[1];
}
else
{
pMax=a[1];
pMin=a[0];
}
i=2;
}
else
{
pMax = pMin =a[0];
i=1;
}
for(;i
if(a[i]>a[i+1])
{
big=a[i];
small=a[i+1];
}
else
{
big=a[i+1];
small=a[i];
}
if(big>pMax)
pMax=big;
if(small
}
}
void main()
{
int n=7;
int a[7] = {4,2,3,4,5,6,7};
int *p ;
int *q ;
cout << "指针" << endl;
MaxAndMin1(n,a,p,q);
cout << "最大值 " << *p << endl;
cout << "最小值 " << *q << endl;
cout << endl;
int p1 = 0;
int q1 = 0;
cout << "引用" << endl;
MaxAndMin2(n,a,p1,q1);
cout << "最大值 " << p1 << endl;
cout << "最小值 " << q1 << endl;
cout << endl;
}
// 借用下上面xxcc309的代码 改了下 时间复杂度变小 空间复杂度变大。。