数据结构

2017-09-07 21:18:49

writer:pprp

第一次作业:

第一道:百钱白鸡问题

第二道:分析复杂度的问题,没有弄好

/*
@theme:百鸡百钱问题
@writer:pprp
@declare:对 x 进行枚举,
@date:2017/9/4
*/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int x[10], y[10], z[10];

    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    memset(z,0,sizeof(z));

    int cnt = 0;

    for(int i = 0 ; i <= 20 ; i++)
    {
        int a = i;
        int c = (300+3*a)/4;
        int b = (100-7*a)/4;
        int d = (300+3*a)%4;
        int e = (100-7*a)%4;
        if(a+b+c == 100 && a >= 0 && a <= 100
                && b >= 0 && b <= 100
                && c >= 0 && c <= 100
                && d == 0 && e == 0)
        {
            x[cnt] = a;
            y[cnt] = b;
            z[cnt] = c;
            cnt++;
        }
    }
    for(int i = 0 ; i < cnt ; i++)
    {
        cout << "ans " << i << ":" << endl;
        cout << x[i] << " " << y[i] << " " << z[i] << endl;
    }
    return 0;
}

第二道题:

课后作业2:

三个学生对同一问题写的算法,测试结果如下:

Case1: n=10的运行时间为

student1: 1

student2: 1/100

student3: 1/1000

Case2: n=100的运行时间为

student1: 10

student2: 1

student3: 1

讨论:哪一个算法最好?

哪一个算法最糟?

为什么?

太小看这个题了,应该从复杂度上进行分析。student1 是 O(n)复杂度,student2是O(n^2)的复杂度,student3是O(n^3)的复杂度

从复杂度角度考虑比较好

第二次作业:顺序表的逆置

/*
@theme: 第二次数据结构作业 - 顺序表的逆置
@writer:pprp
@begin:19:35
@end:20:52
@declare:注意虚函数的使用
@data:2017/9/7
*/

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

const int defaultMaxSize = 100;

template <class T>
class LinearList
{
public:
    LinearList(){}
    ~LinearList(){}
    virtual void Reverse() = 0;
    virtual void input(int sz) = 0;
    virtual void output()const = 0;
};

template <class T>
class SegList : public LinearList<T>
{
private:
    T * data;
    int maxSize;
    int last;
public:
    SegList(int sz = defaultMaxSize)
    {
        if(sz > 0)
        {
            maxSize = sz;
            data = new T[maxSize];
            if(data == NULL)
                cout << "there is no room." << endl;
            last = -1;
        }
    }
    ~SegList()
    {
        maxSize = 0;
        delete []data;
        last = -1;
    }
    void Reverse();//顺序表的逆置
    void input(int sz);//顺序表的输入
    void output()const;//顺序表的输出
};

template <class T>
void SegList<T> :: Reverse()
{
    //两个指针分别指向该顺序表的头和尾
    int stptr = 0;
    int edptr = last;
    for( ; stptr <= edptr; stptr++,edptr--)
    {
        swap(data[stptr],data[edptr]);
    }
    return ;
}

template <class T>
void SegList<T>:: input(int sz)
{
    if(last == maxSize-1)
        cout << " the array is full." << endl, exit(1);
    if(sz < 0 || sz > maxSize)
        cout << " the size of the array is too big/small." << endl, exit(1);
    last = sz - 1;

    cout << "please input the numbers of the array." << endl;
    for(int i = 0 ; i <= last; i++)
    {
        cin >> data[i];
    }
    return ;
}

template <class T>
void SegList<T>::output() const
{
    cout << "the array is as follow:" << endl;
    for(int i = 0 ; i <= last ; i++)
    {
        cout << data[i] << " ";
    }
    cout << endl;
}


int main()
{
    SegList<int> obj;
    cout << "please input the size of array." << endl;
    int sz = 0;
    cin >> sz;
    obj.input(sz);
    obj.output();

    cout << "reverse the array:" << endl;
    obj.Reverse();
    obj.output();

    return 0;
}
原文地址:https://www.cnblogs.com/pprp/p/7491956.html