zoukankan      html  css  js  c++  java
  • 蛋疼

    蛋疼中..于是草草看了一下书 开始琢磨一些无聊的玩意。

    首先是快排。我以前没怎么认真学过数据结构,这两天才稍微好好看了看。不禁感叹快排究竟是何方神圣怎么想出来的。快速排序的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。它的平均性能很不错,是O(nlgn)。实际上,它的运行效果甚至要好于归并排序。实际编程中可以通过c++的标准库直接利用快速排序。实例如下:

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int Compare(const void *elem1, const void *elem2)
    {
    return *((int *)(elem1)) - *((int *)(elem2));
    }
    int main()
    {
    int a[100];
    qsort(a, 100, sizeof(int), Compare);
    int i;
    for(i=0; i<100; ++i)
    cout<<a[i]<<(i%10==9?'\n':'');
    return 0;
    }

    然后是验证几何分布的期望和方差。一切都源于自己的无聊。从温习产生随机种子随机数,到延时,到读键盘,这些虽然以前c++学习的时候都涉及过,现在俨然已忘却。windows.h中,我们可以利用Sleep函数延时,单位是ms。conio.h中,我们可以使用_kbhit()读取键盘缓存,从而进行一些控制工作。进而进行了一些统计工作。

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <windows.h>
    using namespace std;
    int main()
    {
    int a;
    srand(time(0));
    while(1)
    {
    a = rand()%100;
    cout<<a<<endl;
    if (a==0) break;
    Sleep(10);
    }
    }

    从0-99中取数,直到取到0之后停止。假设过程是随机的(虽然机器方法产生的只是伪随机数),那么每一次取到0的概率都是p=1/100。第k次整个过程才结束的概率是(1-p)^(k-1)*p,那么取的次数是符合几何分布的。所以取的次数的数学期望应该是1/p = 100。在这里我偷个懒,不用计数器记录取的次数,而是在程序的执行过程中加入延时语句Sleep()来大概模拟每一次的时间,并且假设每取一次的时间就是10ms。那么程序的execution time也应该大致与取的次数成正比例关系。可以估计,在服从几何分布的前提下,平均执行时间应该是:1/p*10 = 1000ms = 1s,而方差应该是:1-p/(p^2)*100 = 10^6。我无聊地将整个程序运行了若干次,得到如下的一组运行时间:

     343      3635         718        2808         265         140         3401        1997        1763        608        546         796         811         593

     2917    2387         5616      967           448         952         421          47            2604        1576      2075       952         1232        471

     1638     889          78          1732         608         468

    单位是ms

    最终通过简单的处理可以得到期望为:1.3677e+003  方差为: 1.5287e+006

    这个结果与我的理论预期基本上是吻合的。

    另外还做的一件事情就是简单回顾了一下c++里面的计时和读取键盘,关于这部分内容,copy过来的这个程序里面应该有我想要的答案。他完成的工作就是计时,键盘打断,输出。

    #include <iostream>
    #include <time.h>
    #include <conio.h>
    #include <windows.h>

    using namespace std;

    void sleep(unsigned int mseconds)
    {
    clock_t goal = mseconds + clock();
    while (goal > clock());
    }

    void DoPlating()
    {
    int interrupt(0);
    const int interval(1);
    const int duration(1500);
    int elapsed(0);

    unsigned key(1);

    cout << "Start..." << endl;
    cout << "Press any key to break" << endl;

    while(!interrupt && elapsed < duration )
    {
    if(_kbhit())
    {
    interrupt = 1;
    key = getch();
    cout << key << endl;
    }
    sleep(interval);
    elapsed += interval;
    if(_kbhit())
    {
    interrupt = 1;
    key = getch();
    cout << key << endl;
    }
    }

    if( interrupt )
    cout << "Plating interrupted..." << endl;
    else
    cout << "End..." << endl;
    }

    int main()
    {
    DoPlating();
    Sleep(10000);

    return 0;
    }

    回看起来,整个过程都没有太大的意义。但是,在这个我们整日忙着追求意义的今天,偶尔抽点时间出来做点没意义的事情,偶尔虚无一下,感觉还不错~


     

  • 相关阅读:
    Python学习-day10
    python学习-day9
    Python学习-day8
    Python学习-day7
    Python学习-day6
    Django2
    Django讲解
    JQuery讲解
    前端Day2
    前端Day1
  • 原文地址:https://www.cnblogs.com/bovine/p/2186508.html
Copyright © 2011-2022 走看看