编写程序,从键盘上输入3个整形数据,找出其中的最大值和最小值,用指针实现,注意是用指针实现

2025-06-27 02:59:50
推荐回答(2个)
回答1:

#include
using namespace std;

int* Max(int* a, int*b, int* c)
{
int *tmp = *a>*b ? a:b;
return *tmp>*c ? tmp:c;
}

int* Min(int* a, int*b, int* c)
{
int *tmp = *a<*b ? a:b;
return *tmp<*c ? tmp:c;
}

int main()
{
int *a = new int(0);
int *b = new int(0);
int *c = new int(0);

cin>>*a>>*b>>*c;

cout<<"Max: "<<*Max(a,b,c)< cout<<"Min: "<<*Min(a,b,c)<
delete a;
delete b;
delete c;

return 0;
}

回答2:

#include
#include
#define MAX 3

int main(int argc,char **argv)
{
int *num;
int max,min,i;
num=calloc(MAX,sizeof(int));
for(i=0;i printf("please input the %d num:",i);

scanf("%d",num+i);
if(i==0)
max=min=*(num+i);
else{
if(min>*(num+i))
min=*(num+i);
if(max<*(num+i))
max=*(num+i);
}
}
free(num);
printf("the max num is %d\nthe min num is %d\n",max,min);
return 0;
}