数据结构,算法与应用 ---c++语言描述(代码与习题答案),数据结构、算法与应用C++语言描述第一章答案...

正在学习数据结构、算法与应用C++语言描述这本书,将对本书中的习题进行练手与总结。本答案仅供参考,如有错误,还请指正。

本文代码均已用viusual studio 2019进行验证。

题1:

void swap(int& x ,int& y)

{

int temp = x;

x = y;

y = temp;

}

题2:编写一个模板函数count,返回值是[0:n-1]的数值个数。测试你的代码

#include

using namespace std;

template

int count(T* a, int n)

{

int number = 0;

for (int i = 0; i < n; i++)

{

number++;

}

return number++;

}

int main()

{

int n;

cin >> n;

int a[5] = {1,2,3,4,5};

int ans = count(a,n);

cout << ans << endl;

return 0;

}

题3:编写一个模板fill ,给数组a[start ,end -1 ]赋值value。测试你的代码

#include

using namespace std;

template

void fill(T* a, int start, int end, const T &value)

{

for (int i = start; i < end; i++)

{

a[i] = value;

}

return;

}

int main()

{

int a[9] = {1,2,3,4,5,6,7,8,9};

fill(a, 0, 5, 5);

for (auto i : a)

{

cout << i << " ";

}

}

题4:编写一个模板函数inner__product,返回值为内积。测试你的代码

#include

using namespace std;

template

T& inner__product(T* a, T* b, int value)

{

T res = 0;

for (int i = 0; i < value; i++)

{

res += a[i] * b[i];

}

return res;

}

int main()

{

int a[2] = { 1,2 };

int b[2] = { 3,4 };

int ans = inner__product(a, b, 2);

cout << "the inner product is "<

}

题5: 编写一个模板函数iota,使得a[i]= value +i,0<=i

#include

using namespace std;

template

void iota(T* a, const T& value, const int n)

{

for (int i = 0; i < n; i++)

{

a[i] = value + i;

}

return;

}

int main()

{

int a[5];

iota(a, 2, 5);

for (int i = 0; i < 5; i++)

{

cout << "a[" << i << "] is " << a[i] << " "<< std::endl;

}

}

题6:编写一个模板is_sorted,当且仅当a[0:n-1]有序时,返回true。测试你的代码

#include

using namespace std;

template

bool is_sorted(T* a, int n)

{

int upGrade = 1;

int downGrade = 1;

for(int i = 0; i < n-1 ; i++)

{

if (a[i + 1] > a[i])

upGrade++;

else if (a[i + 1] < a[i])

downGrade++;

}

if ((upGrade == n) || (downGrade == n))

return true;

return false;

}

int main()

{

int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

bool ret = is_sorted(a, 9);

cout << "the array a is sored : " << (ret ? "true" : "flase") << endl;

return 0;

}

题7:编写一个函数模板mismatch,返回值是使得a[i]不等于b[i]成立的最小索引i,0 <= i < n。

#include

using namespace std;

template

T& 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()

{

int a[5] = { 1,2,3,4,5 };

int b[5] = { 1,2,3,3,5 };

int c[5] = { 1,2,3,4,5 };

int ans1 = mismatch(a, b, 5);

int ans2 = mismatch(a, c, 5);

if (ans1 == 5)

cout << "the two array are the same" << endl;

else

cout << "the least index is " << ans1 << endl;

if (ans2 == 5)

cout << "the two array are the same" << endl;

else

cout << "the least index is " << ans2 << endl;

}

题8:下面的函数投是具有不同签名的函数吗?为什么?

1.int abc(int a, int b, int c)

2.float abc(int a, int b, int c)

答:是的,因为函数返回类型不同,是abc函数的重载,具有不同签名。

题9:下面语句调用分别调用哪个abc函数?那一句会出现错误?为什么?

1.count<

答:函数调用的签名与程序1.1中abc函数的签名一致。因此,调用程序1.1的abc函数

2.count<

答:函数调用的签名与程序1.2的abc函数的签名一致。因此,调用程序1.2的abc功能

3.count<

答:函数调用的签名与abc函数的签名不一致。由于可以实现从float到int和从int到float的类型转换,c++无法解决使用哪个函数,并给出编译时错误。

4.count<

答:实际参数的类型是double。c++无法解决使用哪个函数,并给出一个编译时错误,因为可以将double类型转换为int和float类型。

题10:修改程序1-8,使抛出的异常类型是整型。如果a,b,c都小于0,那么抛出的异常值是1;如果a,b,c都等于0,那么抛出的异常值是2.否则没有异常。编写一个主函数,应用修改后的代码;若有异常抛出,则捕捉异常;根据异常值输出信息。

#include

using namespace std;

int abc(int a, int b, int c)

{

if (a < 0 && b <0 && c <0)

throw 1;

else if (a == 0 && b == 0 && c == 0)

throw 2;

return a + b * c;

}

int main()

{

int a, b, c;

cin >> a >> b >> c;

try { cout << abc(a, b, c) << endl; }

catch (int e)

{

cout << e << endl;

return 1;

}

return 0;

}

题11:

#include

using namespace std;

template

int count(T* a, int n)

{

if (n < 1)

throw "Error";

int number = 0;

for (int i = 0; i < n; i++)

{

number++;

}

return number++;

}

int main()

{

int n;

int c[5] = {1, 2,3,4,5};

cin >> n;

try {cout << count(c,n) << endl;}

catch(const char* e)

{

cout << e << endl;

return 1;

}

return 0;

}

题12:为程序make2dArray编写一个通用型算法,他的第三个参数不是整数numberOfColumns,而是一位数组rowSize.

template

void make2dArray(T**& x, int numberOfRows, int rowsize[])

{

x = new T * [numberOfRows];

for (int i = 0; i < numberOfRows; i++)

x[i] = new T[rowsize[i]];

}

template

void delete2dArray(T**& x, int numberOfRows)

{

for (int i = 0; i < numberOfRows; i++)

delete[] x[i];

delete[] x;

x = NULL;

}

题13:

#include

using namespace std;

template

void change_length_1d (T* &x, T*& new_x, int old_length, int new_length) {

int size = 0;

if (old_length <= new_length) {

size = old_length;

} else {

size = new_length;

}

new_x = new T [new_length];

for (int i = 0; i < size; ++i) {

new_x[i] = x[i];

}

delete [] x;

}

int main() {

int* old_array = new int [5] { 0, 1, 2, 3, 4 };

int* new_array_smaller;

int* new_array_bigger;

change_length_1d(old_array, new_array_bigger, 5, 6);

for (int i = 0; i < 6; ++i) {

cout << new_array_bigger[i] << " ";

}

cout << endl;

change_length_1d(new_array_bigger, new_array_smaller,6, 2);

for (int i = 0; i < 2; ++i) {

cout << new_array_smaller[i] << " ";

}

return 0;

}

题14:

#include

#include

using namespace std;

template

void changeLength2D(T** a, int oldNum1,int oldNum2, int newNum1,int newNum2)

{

T** temp = new T*[newNum1]; // new array

for(int i=0;i

{

temp[i]=new T[newNum2];

}

int num1 = min(oldNum1, newNum1); // number to copy

int num2=min(oldNum2,newNum2);

for(int i=0;i

{

copy(a[i],a[i]+num2,temp[i]);

}

for(int i=0;i

{

delete [] a[i];// deallocate old memory

}

delete[]a;

a = temp;

for (int i=0;i

for(int j=0;j

cout<

}

int main()

{

int m=3,n=2;

int**a=new int*[m];

for(int i=0;i

{

a[i]=new int[n];

}

for(int i=0;i

for(int j=0;j

a[i][j]=j;

changeLength2D(a,3,2,4,2);

return 0;

}

15题:略

16题:

//currency.cpp

void currency::input()

{

signType thesign;

unsigned long thedollars;

unsigned int thecents;

cin >> (int&)thesign >> thedollars >> thecents;

setValue(thesign, thedollars, thecents);

}

currency currency::subtract(const currency& x) const

{

long a1, a2, a3;

currency result;

a1 = dollars * 100 + cents;

if (sign == sminus)

a1 = -a1;

a2 = x.dollars * 100 + x.cents;

if (x.sign == sminus)

a2 = -a2;

a3 = a2 -a1;

if (a3 < 0)

{

result.sign = sminus;

a3 = -a3;

}

else

result.sign = splus;

result.dollars = a3 / 100;

result.cents = a3 - result.dollars * 100;

return result;

}

currency& currency::percent(double x)

{

long a1, a2;

currency result;

a1 = dollars * 100 + cents;

a2 = (long)(a1 * x / 100);

result.sign = sign;

result.dollars = a2 / 100;

result.cents = a2 - result.dollars * 100;

return result;

}

currency& currency::multiply(double x)

{

currency result;

long a1, a2;

a1 = dollars * 100 + cents;

a2 = a1 * x;

result.sign = sign;

result.dollars = (long)a2 / 100;

result.cents = a2 - result.dollars * 100;

return result;

}

currency& currency::divide(double x)

{

currency result;

long a1, a2;

a1 = dollars * 100 + cents;

a2 = a1 / x;

result.sign = sign;

result.dollars = (long)a2 / 100;

result.cents = a2 - result.dollars * 100;

return result;

}

17题:使用程序1-19的代码完成练习16

//currency.h

void input()

{

double theValue;

cin >> theValue;

setValue(theValue);

}

currency subtract(const currency& x)

{

currency result;

result.amount = amount - x.amount;

return result;

}

currency percent(float x)

{

currency result;

result.amount = (long) (amount * x / 100);

return result;

}

currency multiply(float x)

{

currency result;

result.amount = (long) (amount * x);

return result;

}

currency divide(float x)

{

currency result;

result.amount = (long) (amount / x);

return result;

}

题19:编写非递归程序计算n!,测试你的代码。

#include

using namespace std;

int func1(int n);

int main()

{

int number;

cin >> number;

try { cout << func1(number) << endl; }

catch (const char* e)

{

cout << e << endl;

return 1;

}

}

int func1(int n)

{

int res = 1;

if (n < 0)

throw "error: n <0";

if (n == 0 || n == 1)

return 1;

while (n > 1)

{

res *= n;

n--;

}

return res;

}

题20:

//递归法

int Fibonacci(int n)

{

if (n == 0)

return 0;

else if (n == 1)

return 1;

else

{

return Fibonacci(n - 1) + Fibonacci(n - 2);

}

}

//非递归法

int Fibonacci2(int n)

{

if (n == 0)

return 0;

if (n == 1)

return 1;

int num0 = 0;

int num1 = 1;

int res = 0;

for (int i = 1;i < n; i++)

{

res = num1 + num0 ;

num0 = num1;

num1 = res;

}

return res;

};

题21:

//递归法

int function(int n)

{

if (n % 2 == 0)

return n / 2;

if (n % 2 == 1)

return function(3 * n + 1);

}

//非递归法

int function2(int n)

{

if (n % 2 == 0)

return n / 2;

if (n % 2 == 1)

return n + (n + 1) / 2;

}

题22:

int Ackermann(int i,int j)

{

if(i==1&&j>=1)

return pow(2,double(j));

else if(i==2&&j==1)

return Ackermann(i-1,2);

else if(i>=2&&j>=2)

return Ackermann(i-1,Ackermann(i,j-1));

}

题23:

int gcd(int x,int y)

{

if(x

{

int temp=x;

x=y;

y=temp;

}

if(y==0)

return x;

else

return gcd(y,x%y);

}

题24:

template

bool function(const vector& a, int x, int pos)

{

int n = a.size();

if (pos >= n)

return false;

if (x == a[pos] && pos < n)

return true;

else

return in_the_array(a, x, pos + 1);

}

题25:

template

void Subset_Generation(vector a, int n, vector b) {

if (n == 0) {

for (int i = 0; i < b.size(); i++)

cout << b[i];

cout<

}

else

{

b[n-1]=0;

Subset_Generation(a,n-1,b);

b[n-1]=1;

Subset_Generation(a,n-1,b);

}

}

题26:

void function(int n)

{

if(n==1)

cout<<1;

else

{

function(n-1);

cout<

function(n-1);

}

}

本文持续更新中......

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构算法应用是一门关于数据组织、存储和处理的课程,它是计算机科学与技术中非常重要的一部分。在该课程中,C语言是一种常用的编程语言,用于描述、实现和应用各种数据结构算法习题答案通常包括对于不同算法数据结构的实现和分析。在使用C语言描述习题答案时,我们需要先了解所涉及的数据结构算法的基本概念和原理,并掌握C语言的编程特性和语法规则。 在C语言中,我们可以使用各种数据结构来解决不同的问题,例如数组、链表、栈、队列、树、图等等。对于每个数据结构,我们需要实现其基本的操作,例如插入、删除、查找等。同时,我们还需要熟悉不同的算法,如排序、查找、图遍历等,以及它们的时间和空间复杂度分析。 对于习题答案描述,我们可以使用C语言编写对应的代码,并给出相应的算法数据结构的解释。例如,对于一个排序算法习题,我们可以使用C语言中的排序函数库来实现排序,然后分析算法的时间和空间复杂度。对于一个图算法习题,我们可以使用C语言中的图表示来实现算法,并分析算法的效率和正确性。 总之,使用C语言描述数据结构算法应用习题答案需要掌握C语言的编程技巧和数据结构算法的基本知识,能够将算法数据结构的理论知识实际应用到解决实际问题中。这不仅对提高我们的编程能力和算法思维有很大帮助,也对我们的职业发展和学术研究有着重要的意义。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值