资源简介
输油管道问题,在VC6.0中实现,算法参考《计算机算法设计与分析》(王晓东)。分治算法RandomizedSelect

代码片段和文件信息
#include
using std::cout;
using std::endl;
using std::cin;
int Random(int pint r)
{
// srand((unsigned)time(0));
int a=rand()%(r-p+1)+p;
return a;
}
void Swap(int &aint &b)
{ int m;
m=a;
a=b;
b=m;
}
int Partition(int a[]int pint r)
{
int i=pj=r+1;
int x=a[p];
while(true)
{
while (a[++i] while (a[--j]>x);
if (i>=j) break;
Swap(a[i]a[j]);
}
a[p]=a[j];
a[j]=x;
return j;
}
int RandomizedPartition(int a[]int pint r)
{
int i=Random(pr);
Swap(a[i]a[p]);
return Partition(apr);
}
int RandomizedSelect(int a[]int pint rint k)
{ int j;
if(p>=r) return a[p];
int i=RandomizedPartition(apr);
j=i-p+1;
if(k==j) return a[i];
if(k else return RandomizedSelect(ai+1rk-j);
}
int r=0;
int main(){
printf(“请输入油井的个数:“);
cin>>r;
int *x=new int[r];
int *y=new int[r];
for(int i=0;i {
printf(“请输入第%d个管道的坐标:“i+1);
cin>>x[i];
cin>>y[i];
}
int p=0b=0c=0;
int k=(r+1)/2;
b=RandomizedSelect(ypr-1k);
for(int j=0;j c+=abs(b-y[j]);
cout< return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-05-22 17:15 shuyou\Debug
文件 1253 2012-05-22 12:57 shuyou\shuyou.cpp
文件 4284 2012-05-22 09:06 shuyou\shuyou.dsp
文件 520 2012-05-22 08:53 shuyou\shuyou.dsw
文件 41984 2012-05-22 17:14 shuyou\shuyou.ncb
文件 48640 2012-05-22 17:14 shuyou\shuyou.opt
文件 1276 2012-05-22 17:13 shuyou\shuyou.plg
目录 0 2012-05-22 17:14 shuyou
----------- --------- ---------- ----- ----
97957 8
- 上一篇:c++实现词法分析器
- 下一篇:c++语言-物流管理系统
相关资源
- CCS FFT c语言算法
- 小波变换算法 c语言版
- 3des加密算法C语言实现
- 单片机c语言源程序(51定时器 八个按
- DES加密算法C语言实现
- 线性回归算法c语言实现
- 基于C语言的模拟退火算法
- C语言实现的DES对称加密算法
- 用VC6.0实现多边形扫描线填充算法
- c语言编写的货郎担算法
- Em算法(使用C++编写)
- STM32烧写算法flash包
- 永磁同步电机的FOC控制算法
- Proteus仿真:PID算法输出.rar
- 一个模糊PID温度控制算法源代码
- 经典滤波算法
- KMP算法C语言程序
- SVM算法实现(源码+文档)
- 算法A律U律实现
- 人工蜂群算法.docx
- aes加解密(vc源程序)
- BlowFish加密算法
- C语言常用算法源代码
- c++数组快排算法
- 算法表达式求值.cpp
- PID算法.c
- QR二维码C++源码 算法实现
- 基于opencv漫水填充算法综合
- 信息学奥赛一本通——算法部分
- 银行家算法分配资源的模拟实现(m
评论
共有 条评论