C++ OJ习题练习(三)函数重载

Problem Description

编写重载函数min(),分别计算int、double、float、long类型数组中的最小值。
程序如下,请完善该程序的设计:

#include 
using namespace std;
int min(int [],int);
double min(double[],int);
float min(float[],int);
long min(long[],int);
int main(){
         int a[6]={2,22,0,-6,67,-111};
         int aa[4]={5,19,2,28};
         double b[8]={2.2,62,-6.1,500,68.2,-500.345,-8,1000};
         float c[4]={3.2,-8.61,699,33};
         long d[3]={3265891,14789,-63256};
         cout<<"the least number in a[6] is "<<min(a,6)<<endl;
         cout<<"the least number in b[8] is "<<min(b,8)<<endl;
         cout<<"the least number in c[4] is "<<min(c,4)<<endl;
         cout<<"the least number in d[3] is "<<min(d,3)<<endl;
         cout<<"the least number in aa[4] is "<<min(aa,4)<<endl;
         return 0;
}
//你的代码将被嵌在这里

Sample Output

the least number in a[6] is -111
the least number in b[8] is -500.345
the least number in c[4] is -8.61
the least number in d[3] is -63256
the least number in aa[4] is 2

解题代码

#include 
using namespace std;
int min(int [],int);
double min(double[],int);
float min(float[],int);
long min(long[],int);
int main(){
         int a[6]={2,22,0,-6,67,-111};
         int aa[4]={5,19,2,28};
         double b[8]={2.2,62,-6.1,500,68.2,-500.345,-8,1000};
         float c[4]={3.2,-8.61,699,33};
         long d[3]={3265891,14789,-63256};
         cout<<"the least number in a[6] is "<<min(a,6)<<endl;
         cout<<"the least number in b[8] is "<<min(b,8)<<endl;
         cout<<"the least number in c[4] is "<<min(c,4)<<endl;
         cout<<"the least number in d[3] is "<<min(d,3)<<endl;
         cout<<"the least number in aa[4] is "<<min(aa,4)<<endl;
         return 0;
}
//你的代码将被嵌在这里
int min(int arr[],int n)
{
   int min = arr[0];
   for(int i = 1;i < n;i++)
      if(min > arr[i]) min = arr[i];
   return min;
}

double min(double arr[],int n)
{
   double min = arr[0];
   for(int i = 1;i < n;i++)
      if(min > arr[i]) min = arr[i];
   return min;
}

float min(float arr[],int n)
{
   float min = arr[0];
   for(int i = 1;i < n;i++)
      if(min > arr[i]) min = arr[i];
   return min;
}

long min(long arr[],int n)
{
   long min = arr[0];
   for(int i = 1;i < n;i++)
      if(min > arr[i]) min = arr[i];
   return min;
}

你可能感兴趣的:(C/C++,c++,oj系统)