数据结构、算法与应用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.编译失败,调用不明确