用重载求距离.

用重载求距离.

时间: 1ms        内存:128M

描述:

使用函数重载的方法定义两个重名函数,分别求出整型数的两点间距离和浮点型数的两点间距离.只需提交两个函数即可。

#include <iostream>
#include <cmath>
using namespace std;
double func(int,int,int,int),func(double,double,double,double);
int  main( )
{ cout<<func(2,2,5,5)<<endl;
  cout<<func(2.2,2.2,5.5,5.5)<<endl;
return 0;}

输入:

输出:

示例输入:

示例输出:

4.24264
4.6669

提示:

参考答案(内存最优[0]):


#include <iostream>
#include <cmath>
using namespace std;
double func(int,int,int,int),func(double,double,double,double);
int  main( )
{ cout<<func(2,2,5,5)<<endl;
  cout<<func(2.2,2.2,5.5,5.5)<<endl;
return 0;}
double func(int x1,int y1,int x2,int y2)
{return sqrt((x1-x2)* (x1-x2)+(y1-y2)*(y1-y2));}
double func(double x1,double y1,double x2,double y2)
{return sqrt((x1-x2)* (x1-x2)+(y1-y2)*(y1-y2));}

参考答案(时间最优[0]):


#include <iostream>
#include <cmath>
using namespace std;
double func(int,int,int,int),func(double,double,double,double);
int  main( )
{ cout<<func(2,2,5,5)<<endl;
  cout<<func(2.2,2.2,5.5,5.5)<<endl;
return 0;}
double func(int x1,int y1,int x2,int y2)
{return sqrt((x1-x2)* (x1-x2)+(y1-y2)*(y1-y2));}
double func(double x1,double y1,double x2,double y2)
{return sqrt((x1-x2)* (x1-x2)+(y1-y2)*(y1-y2));}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注