数据结构、算法与应用 C++语言描述 第一章课后习题

数据结构、算法与应用 C++语言描述 第一章课后习题

Q12

#include <iostream>

using namespace std;

template <class T>
void make2dArray(T ** &x, int numberOfrows, int *rowSize)
{
    // 先将x实例化,传入的参数只有一个名字,没有实际位置
    x = new T *[numberOfrows];
    for(int i=0;i<numberOfrows;++i)
        x[i] = new T [rowSize[i]];
}

template<class T>
void delete2dArray(T ** &x, int numberOfrows)
{
    for(int i=0;i<numberOfrows;++i)
        delete [] x[i];
    delete [] x;
    x=NULL;
}

int main()
{
    // 当行维度和列维度都未知时,利用指针的指针进行
    int ** x;
    int rows=5;
    int rowSize[]={3,4,8,6,3};
    make2dArray(x, rows, rowSize);
    x[2][6]=5;
    cout<< x[2][6]<<endl;
    delete2dArray(x, rows);
    return 0;
}

Q19

#include <iostream>

using namespace std;

int multi(int n)
{
    if(n<2)
        return 1;
    else
        return n * multi(n-1);
}

int main()
{
    int result = multi(10);
    cout<<result<<endl;
    return 0;
}

Q20

#include <iostream>

using namespace std;

int F(int n)
{
    if (n==0)
        return 0;
    else if(n==1)
        return 1;
    else
        return F(n-1) + F(n-2);
}

int main()
{
    cout<<F(10)<<endl;
    return 0;
}

Q21

#include <iostream>

using namespace std;

int F(int n)
{
    if(n%2==0)
        return n/2;
    else
        return F(3*n+1);
}

int main()
{
    cout<<F(9)<<endl;
    return 0;
}

Q22

#include <iostream>
#include <math.h>

using namespace std;

int A(int i, int j)
{
    if(i==1&&j>=1)
        return pow(2, j);
    else if(i>=2&&j==1)
        return A(i-1,2);
    else
        return A(i-1,A(i,j-1));
}

int main()
{
    cout<<A(2,2)<<endl;
    return 0;
}

Q23

#include <iostream>

using namespace std;

int gcd(int x, int y)
{
    if(y==0)
        return x;
    else
    {
        return gcd(y, x % y);
    }
        
}

int main()
{
    cout<<gcd(112, 42)<<endl;
    return 0;
}

Q24

#include <iostream>

using namespace std;

template <class T>
bool findX(T a, T* arr, int n, int N)
{
    if(a == arr[n])
        return true;
    else if(n == N)
        return false;
    else
        return findX(a, arr, n+1, N);
}

int main()
{
    int N=7;
    int arr[]={0, 8, 69, 6, 7, 35, 4};
    cout<<findX(36, arr, 0, N) <<endl;
    return 0;
}

25

#include <iostream>
using namespace std;

// k是每个循环的起始位置,N是数组总长度
void getSubset(int *arr, int k, int N)
{
    if(k == N)
    {
        for(int j=0;j<N;j++)
        {
            cout << arr[j];
        }
        cout << endl;
        return;
    }

    if(k < N)
    {
        arr[k] = 0;
        getSubset(arr, k+1, N);
        arr[k] = 1;
        getSubset(arr, k+1, N);
    }
}

int main()
{
    int arr[]={0,0,0};
    int N = 3;
    getSubset(arr, 0, N);
    return 0;
}

26

#include <iostream>
using namespace std;

void G(int n)
{
    if(n == 1)
    {
        cout << 1;
    }
    else
    {
        G(n-1);
        cout << n;
        G(n-1);
    }
}

int main()
{
    G(5);
    return 0;
}

27

#include <iostream>
#include <vector>
using namespace std;

template <class T>
T accumulate(typename vector<T>::iterator start, typename vector<T>::iterator end, T initialValue)
{
    for(;start < end; start++)
    {
        initialValue += *start;
    }
    return initialValue;
}

int main()
{
    vector<double> vec={0.1, 6.3, 9.0, 5, 4.8, 3.5};
    double initialValue=0.0;
    double sum = accumulate(vec.begin(), vec.end(), initialValue);
    cout << sum << endl;
    return 0;
}

Q28

#include <iostream>
#include <vector>
using namespace std;

template <class T>
T accumulate(typename vector<T>::iterator start, typename vector<T>::iterator end, T initialValue, T (*pf)(T &a, T &b))
{
    for(;start < end; ++start)
    {
        initialValue = pf(initialValue, *start);
    }
    return initialValue;
}

template <class T>
T multi(T &a, T &b)
{
    return a * b;
}

template <class T>
T add(T &a, T &b)
{
    return a + b;
}

int main()
{
    vector<double> vec={0.1, 6.3, 9.0, 5, 4.8, 3.5};
    double initialValue=1.0;
    double sum = accumulate(vec.begin(), vec.end(), initialValue, multi);
    cout << sum << endl;

    sum = accumulate(vec.begin(), vec.end(), initialValue, add);
    cout << sum << endl;
    return 0;
}

29

#include <iostream>
#include <vector>
using namespace std;


// 利用内置数组类型进行操作
template <class T>
bool copy(T *start, T *end, T *aim)
{
    for(;start<end; ++start, ++aim)
    {
        *aim = *start;
    }
    return true;
}

int main()
{
    int a[] = {5, 9, 8, 3, 7, 5};
    int b[sizeof(a)/sizeof(*a)];
    cout << copy(begin(a), end(a), begin(b)) << endl;
    cout << b[5] << endl;
    return 0;
}

30

#include <iostream>
#include <ostream>
#include <iterator>
#include <algorithm>
using namespace std;

char str[]={'k', 'a', 'g', 'n', 'f', 'b','w','h'};
// int str[] = {5, 9, 8, 3, 7, 5};
template <class T>
void permutations(T list[], int &k, int m)
{
    sort(list, list + m);
    do
    {
        copy(list, list + m, ostream_iterator<T>(cout, ""));
        cout<<endl;
        k++;
    } while (next_permutation(list, list + m));;
}

int main()
{
    int num=0;
    permutations(str, num, 8);
    cout << num<<endl;
    return 0;    
}

32

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// template <class T>
// int count(T *start, T *end, T value)
// {
//     int num=0;
//     for(;start<end;start++)
//     {
//         if(*start==value)
//             num++;
//     }
//     return num;
// }

int main()
{
    char arr[]={'g', 'g', 'h', 'd', 'g', 'e', 'h'};
    cout << count(arr, end(arr), 'h') << endl;
    return 0;
}
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第一章 绪论作业答案(共50分) 一、分析如下程序中 (1)~ (10)各语句的频度。(每个1分,共10分) Ex( ) { int i , j , t ; (1) for( i=1 ; i<10 ; i++) //n = (2) printf(“\n %d” , i ); //n = (3) for(i=1; i<=2; i++) //n = (4) printf(“\n”); //n = (5) for(i=1; i<=9; i++) //n = { (6) for(j=1; j <= i ; j++) //n = { (7) t = i * j ; //n = (8) printf(“]”,t); //n = } (9) for(j=1; j 0) { if(x > 100) {x -= 10 ; y -- ;} else x ++ ; } 问if 语句执行了多少次?(2分) y--执行了多少次?(2分) x ++执行了多少次?(2分) 三、回答问题(共25分) 书中16页的起泡排序如下: void bubble_sort(int a[],int n){ //将a中整数序列重新排列成自小至大有序的整数序列。 for(i=n-1,change=TRUE;i>=1&&change;--i){ change=FALSE; for(j=0;ja[j+1]{a[j]<-->a[j+1];change=TRUE; } } }//bubble_sort 1.(共15分)分析该算法的最佳情况 ,最坏情况和平均情况下各自的时间复杂度(给出分析思路与过程)。 (1) 最佳情况的时间复杂度分析(5分): (2) 最坏情况的时间复杂度分析(5分): (3) 平均情况的时间复杂度分析(5分): 2.(共10分)比较与C语言书中的起泡排序异同,并从时空效率角度说明谁更优。 四、完成如下选择题(每3分,共9分)。 1. 设f为原操作,则如下算法的时间复杂度是( )。 for (i = 1; i*i=1;i--) for(j=1;jA[j+1]) A[j]与A[j+1]对换; 其中n为正整数,则算法在最坏情况下的时间复杂度为( )。 A.O(n) B.O(nlog2n) C. O(n3) D. O(n2)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值