【职业规划】编程之美系列1

1-1

#include <iostream>
#include "windows.h"
#include "stdlib.h"
#include "math.h"
#pragma execution_character_set("utf-8")

#define GetCPUTickCount() _rdtsc()

int main()
{
    
    const int samping_count = 200;
    const int total_amplitude = 300;  //300ms
    const double P = 3.14156265;

        DWORD busyspan[samping_count];  //DWORD 32位无符号整数
        int amplitude = total_amplitude / 2;
        double radian = 0.0;
        double radianincrement = 2.0 /(double)samping_count;
        for(int i = 0;i < samping_count;i++){
            busyspan[i] = (DWORD)(amplitude + (sin(P * radian) * amplitude));
            radian += radianincrement;
        }
        DWORD starttime = 0;
        for(int j = 0;;j = (j + 1) % samping_count){
            starttime = GetTickCount();
            while(GetTickCount() - starttime <= busyspan[j]);
            Sleep(total_amplitude - busyspan[j]);
        }
    

    /*
    cout << "Beauty of Programming 1-1" << endl;

    CONST DWORD busytime = 10;
    CONST DWORD idletime = busytime;
    int starttime = 0;
    while(true){
        DWORD starttime = GetTickCount();
        cout << GetTickCount() << endl;
        while(GetTickCount() - starttime <= busytime);
        Sleep(idletime);
    }
    */

    /*
    for(;;){
        for(int i = 0;i < 9600000;i++);
            Sleep(10);
    }
    */
    return 0;
}

1-2

#include <iostream>
#include "windows.h"
#include "stdlib.h"
#include "math.h"
#include <cmath>
using namespace std;

int main()
{
    cout << "Beauty of Programming 1-2" << endl;

    int s_time = GetTickCount();

    unsigned char i = 81;
    while(i--){
        if(i / 9 % 3 == i % 9 % 3)  //0-8的位置,
            continue;
        cout << "A = " << i / 9 + 1 << "B = " << i % 9 + 1 << endl;
    }

    int e_time = GetTickCount();
    int time = e_time - s_time;
    cout << "time1 = " << time << endl;

    s_time = GetTickCount();

    struct{
        unsigned char a:4;
        unsigned char b:4;
    }j;

    for(j.a = 1;j.a <= 9;j.a++)
        for(j.b = 1;j.b <=9;j.b++)
            if(j.a % 3 != j.b % 3)  //0-8的位置,
            cout << "A = " << (int)j.a << "B = " << (int)j.b << endl;

    e_time = GetTickCount();
    time = e_time - s_time;
    cout << "time2 = " << time << endl;

    return 0;
}

1-3 (无法正常运行待解决)

#include <iostream>
#include "windows.h"
#include "stdlib.h"
#include "math.h"
#include <cmath>
#include <cassert>

using namespace std;

class CPrefixSorting{
public:
    CPrefixSorting(){
        m_nCakeCnt = 0;
        m_nMaxSwap = 0;
    }
    ~CPrefixSorting(){
        if(m_CakeArray != NULL)
            delete m_CakeArray;
        if(m_SwapArray != NULL)
            delete m_SwapArray;
        if(m_ReverseCakeArray != NULL)
            delete m_ReverseCakeArray;
        if(m_ReverseCakeArraySwap != NULL)
            delete m_ReverseCakeArraySwap;
    }
    void Run(int* pCakeArray, int nCakeCnt){
        Init(pCakeArray, nCakeCnt);  //烙饼索引数组pCakeArray, 烙饼个数nCakeCnt
        m_nSearch = 0;
        Search(0);
    }
    void Output(){
        for(int i =0; i < m_nMaxSwap; i++)
            cout << m_SwapArray[i] << endl;
        cout << "search times" << m_nSearch << endl;
        cout << "total swap times" << m_nMaxSwap << endl;
    }
private:
    void Init(int* pCakeArray, int nCakeCnt){
        assert(pCakeArray != NULL);
        assert(nCakeCnt > 0);

        m_nCakeCnt = nCakeCnt;
        m_CakeArray = new int[m_nCakeCnt];
        assert(m_CakeArray != NULL);
        for(int i =0;i < m_nCakeCnt;i++)
            m_CakeArray[i] = pCakeArray[i];
        m_nMaxSwap = UpperBound(m_nCakeCnt);  //最多交换次数
        m_SwapArray = new int[m_nMaxSwap + 1];  //交换结果数组
        assert(m_SwapArray != NULL);
        m_ReverseCakeArray= new int[m_nCakeCnt];  //中间交换结果
        for(int i = 0;i < m_nCakeCnt;i++)
            m_ReverseCakeArray[i] = m_CakeArray[i];
        m_ReverseCakeArraySwap = new int[m_nMaxSwap];
    }
    int UpperBound(int nCakeCnt){
        return nCakeCnt * 2;
    }
    int LowerBound(int* pCakeArray, int nCakeCnt){
        int t, ret = 0;
        for(int i = 1;i < nCakeCnt;i++){
            t = pCakeArray[i] - pCakeArray[i-1];
            if(t == 1||t == -1);
            else
                ret++;
        }
        return ret;
    }
    void Search(int step){  //递归排序
        int i, nEstimate;
        m_nSearch++;
        nEstimate = LowerBound(m_ReverseCakeArray, m_nCakeCnt);
        if(step + nEstimate > m_nMaxSwap)
            return;
        if(IsSorted(m_ReverseCakeArray, m_nCakeCnt)){
            if(step < m_nMaxSwap){
                m_nMaxSwap = step;
                for(i = 0;i < m_nMaxSwap;i++)
                    m_SwapArray[i] = m_ReverseCakeArraySwap[i];
            }
            return;
        }
        for(i = 1;i < m_nCakeCnt;i++){
            Reverse(0,i);
            m_ReverseCakeArraySwap[step] = i;
            Search(step + 1);
            Reverse(0,i);
        }
    }
    bool IsSorted(int* pCakeArray, int nCakeCnt){
        for(int i =1;i < nCakeCnt;i++){
            if(pCakeArray[i-1] > pCakeArray[i])
                return false;
        }
        return true;
    }
    void Reverse(int nBegin, int nEnd){
        assert(nEnd > nBegin);
        int i,j,t;
        for(i = nBegin,j = nEnd;i<j;i++,j--){
            t = m_ReverseCakeArray[i];
            m_ReverseCakeArray[i] = m_ReverseCakeArray[j];
            m_ReverseCakeArray[j] = t;
        }
    }
private:
    int* m_CakeArray;
    int m_nCakeCnt;
    int m_nMaxSwap;
    int* m_SwapArray;
    int* m_ReverseCakeArray;
    int* m_ReverseCakeArraySwap;
    int m_nSearch;
};

int main()
{
    int c;
    int* p;
    cout << "1-3" << endl;
    CPrefixSorting cp;
    cout << "num input:" << endl;
    cin >> c ;
    cout << "cp input" << endl;
    for(int i = 0;i < c;i++)
        cin >> p[i];
    cp.Run(p, c);
    cp.Output();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值