数据结构、算法与应用c++语言描述(答案)

数据结构、算法与应用c++语言描述(答案)

https://www.cise.ufl.edu/~sahni/dsaac/view.htm

不知道为什么作者放的答案只有一半,,下边自己写的没有答案的部分,有错误请指正。

chapter 1  01

void swap(int& x, int& y);

chapter 1  02

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <typename T>
int count(const vector<T> &list, const T &value) 
{
    int sum = 0;
    for(auto it = list.begin(); it!=list.end();it++)
    {
        if(*it == value)
        {
            sum++; 
        }
    }
    return sum;
}
int main() 
{
    vector<int> list_int = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    std::cout << "list_int Count is " << count(list_int, 0) << std::endl;
    vector<string> list_str = {"a", "b", "c", "c", "c"};
    string str = "c";
    std::cout << "list_str Count is " << count(list_str, str) << std::endl;
    return 0;
}

chapter 1  03

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <class T>
void fill(vector<T>& a, int start, int end, const T& value)
{
   if(end >= a.size())
   {
        end = a.size();
   }
   for (int i = start; i < end; i++)
      a[i] = value;
}
template <class T>
void printfVector(const vector<T>& list)
{
    cout<<"{";
    for(auto it = list.begin(); it!=list.end(); it++)
    {
        cout<<*it<<" ";
    }
    cout<<"}"<<endl;;
}
int main() 
{
    vector<int> list_int = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    fill(list_int, 3,6, 0);
    printfVector(list_int);

    vector<string> list_str = {"a", "b", "c", "c", "c"};
    const string str = "hello";
    fill(list_str, 3,6, str);
    printfVector(list_str);

    return 0;
}

chapter 1  04

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <class T>
T inner_product(const vector<T>& a, const vector<T>& b )
{
    if(a.size() != b.size())
    {
        return 0;
    }
    T sum = 0;
    for(int i=0;i<a.size();i++)
    {
        sum += a[i]*b[i];
    }
    return sum;
}
int main() 
{
    vector<int> list_int1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    vector<int> list_int2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    cout<<inner_product(list_int1, list_int2)<<endl;


    vector<double> list_double1 = {1.1,22,33,2.2,7.3};
    vector<double> list_double2 = {1.1,22,33,2.2,7.3};
    cout<<inner_product(list_double1, list_double2)<<endl;

    return 0;
}

chapter 1  05

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <class T>
void iota(vector<T>& a, int n, const T& value)
{
   for (int i = 0; i < n; i++)
      a[i] += value;
}
template <class T>
void printfVector(const vector<T>& list)
{
    cout<<"{";
    for(auto it = list.begin(); it!=list.end(); it++)
    {
        cout<<*it<<" ";
    }
    cout<<"}"<<endl;;
}
int main() 
{
    const int value1 = 2;
    vector<int> list_int1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    iota(list_int1, 3, value1);
    printfVector(list_int1);

    const double value2 = 1.1;
    vector<double> list_double1 = {1.1,22,33,2.2,7.3};
    iota(list_double1, 3, value2);
    printfVector(list_double1);

    return 0;
}

chapter 1  06

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <typename T>
bool is_sorted(const vector<T> &list, const int& n) 
{
    for (int i = 0; i < n - 1; i++)
    {
        if (list[i] > list[i + 1]) 
        {
            return false;
        }
    }
    return true;
}

int main() 
{
    vector<int> list_int = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    cout << is_sorted(list_int, 13)<<endl;
    cout << is_sorted(list_int, 10)<<endl;
    list_int[3] = 0;
    cout << is_sorted(list_int, 10)<<endl;
    return 0;
}

chapter 1  07

#include <vector>
#include <string>
#include <iostream>
using namespace std;
template <class T>
int mismatch(T& a, T& b, int n)
{
   for (int i = 0; i < n; i++)
   {
        if (a[i] != b[i])
        return i;
   }
    
   return n;
}

int main() 
{
    vector<int> list_int1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
     vector<int> list_int2 = {1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0};
    cout << mismatch(list_int1, list_int2, 5)<<endl;
    return 0;
}

chapter 1  08

不是,形参的类型,数量完全相同

chapter 1  09

1.1-1 

 2.1-2 

3.编译失败,调用不明确

4.编译失败,调用不明确

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值