_(:△」∠)_ 2021-10-08 16:43 采纳率: 47.5%
浏览 20
已结题

这是一个平面最近点对问题,我有一个产生随机数并存入文本的操作(在主函数中),然后读取文本内容制成点的坐标,然后我这个动作是通过k来进行循环的,那么为什么它每次测试结果的坐标都一样应该是不一样的才对

#include<iostream>
#include<math.h>  //计算距离
#include<time.h>  //计算运行时间
#include<fstream>  //文件操作
#include<algorithm>  //排序
#include <stdlib.h>

//#include <windows.h>
#include <cstdio>
#include <cstdlib>


using namespace std;
#define MAX 0x3f3f3f3f  //定义无穷大
#define M 99999
 
struct point {
  double x, y;
}p[M];
 
int a[M];// 保存排序的索引
 
int cmpx(const point& a, const point& b) //x标轴排序升序
{  
  return a.x < b.x;
}
 
int cmpy(int &a, int &b) //y轴排序升序
{
  return p[a].y < p[b].y;
}
 
inline double min(double a, double b) //返回两个值中较小的
{
    return a < b ? a : b;
}

inline double dist(const point& a, const point& b)//计算两点之间的距离
{
  return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}


void ExportData(double n)//时间存入文本,手动建立一个叫做out.txt
{
    ofstream FF("out.txt", ios::in|ios::binary|ios::ate|ios::app);
    FF<<n<<"\r\n";
    FF.close();
}


double closeset(int low, int high)
{
    if (low == high)   
        return MAX;
    if (low + 1 == high)  //即n=2,返回两点之间的距离
        return dist(p[low], p[high]);
    int mid = (low + high)>>1;  //右移一位,相当于除以2,但右移的运算速度更快,若使用(low+high)/2求中间位置容易溢出
    double ans = min(closeset(low, mid), closeset(mid+1, high));  //递归求出两边最小距离
    int i, j, c = 0;
    for (i = low; i <= high; i++) //统计那些点位于两虚线内,并记录
    {
        if (p[mid].x - ans <= p[i].x && p[i].x <= p[mid].x + ans)
            a[c++] = i;
    }
    sort(a, a + c, cmpy);
    for(i = 0; i < c; i++)//比较s1中虚线内的点和s2中虚线内的点的距离是否存在有小于两侧最小对的
    {
        int k = i+7 > c ? c : i+7;  
        for (j = i+1; j < k; j++)
        {
            if (p[a[j]].y - p[a[i]].y > ans)  //如果位于中位线两侧的点的距离大于anx则跳出第一个循环
                break;
            ans = min(dist(p[a[i]], p[a[j]]), ans); //如果有两个点小于两侧的最近对,则选出最小的点
        }
    }
    return ans;
}
 
int main()
{   int k,m;//
    //long BegainTime ; //
    //long EndTime ;//
    cout<<"输入测试次数:";//
    cin>>m;//
    int n;//一共多少个点
    cout<<"读入点数为:";
    cin>>n;


    for(k=1;k<=m;k++)//
    {//
    cout<<"第"<<k<<"次测试中......"<<endl;//
    clock_t start;
    double totaltime;
    start=clock();
//-------------

     fstream fso("close.txt", ios::out|ios::ate|ios::trunc); //手动建立一个叫做out.txt
      srand(time(NULL));//随机数生成器播散种子没有这个产生的随机数不变
      for(int i1 = 0; i1<(2*n); ++i1){
          fso << rand()%1000<<' ';
      }
      fso.close();


//-------------------
    double dmin;
    //ifstream read_in;
    //read_in.open("close.txt");
    fstream fsi("close.txt", ios::in);
    cout<<"点的坐标为:"<<endl;
    for(int i=0;i<n;i++)  //循环读入文件
    {
        cout<<"p"<<i+1<<":";
        //read_in>>p[i].x>>p[i].y;
        fsi>>p[i].x>>p[i].y;
        cout<<p[i].x<<" "<<p[i].y<<endl;
    }

    sort(p,p+n,cmpx); //按照x轴排序
    dmin=closeset(0, n-1);
    cout<<"最近的距离是:"<<dmin<<endl;
    clock_t end=clock();
    totaltime=(double)(end-start)/CLOCKS_PER_SEC;
    cout<<"程序运行时间是:"<<totaltime<<endl;
    ExportData((double)(end-start)/CLOCKS_PER_SEC);//

    cout<<"第"<<k<<"次测试完成。"<<endl;//
    }//
    return 0;
}

img

  • 写回答

1条回答 默认 最新

  • orange4reg 2021-10-08 17:00
    关注

    srand(time(NULL));一般来说相隔比较久的话用一次就好了,连续用,时间相隔太短,等于随机里面的数据重置了。把srand(time(NULL));放到第一个for之前就好了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月12日
  • 已采纳回答 10月8日
  • 创建了问题 10月8日

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”