数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大.doc

上传人:asd****56 文档编号:69697729 上传时间:2023-01-07 格式:DOC 页数:43 大小:166KB
返回 下载 相关 举报
数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大.doc_第1页
第1页 / 共43页
数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大.doc_第2页
第2页 / 共43页
点击查看更多>>
资源描述

《数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大.doc》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大.doc(43页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Chapter 10Chapter 10: ADT Implementations: Recursion, Algorithm Analysis, and Standard AlgorithmsProgramming ProblemsSection 10.1 1./*- Driver program to test recursive digit-counting function of Exercise 21. Input: Nonnegative integers Output: Number of digits in each integer -*/#include using name

2、space std;int numDigits(unsigned n);/*- Recursively count the digits in a nonnegative integer - Exer. 21. Precondition: n = 0 Postcondition: Number of digits in n is returned. -*/int main() int n; for (;) cout n; if (n 0) break; cout # digits = numDigits(n) endl; /- Definition of numDigits()int numD

3、igits(unsigned n) if (n 10) return 1; else return 1 + numDigits(n / 10);2.Just replace the function definition in 1 with that in Exercise 22.3./*- Driver program to test reverse-print function of Exercise 23. Input: Nonnegative integers Output: The reversal of each integer. -*/#include using namespa

4、ce std;void printReverse(unsigned n);/*- Recursively display the digits of a nonnegative integer in reverse order - Exer. 23. Precondition: n = 0 Postcondition: Reversal of n has been output to cout. -*/int main() int n; for (;) cout n; if (n 0) break; cout Reversal is: ; printReverse(n); /- Definit

5、ion of printReverse()void printReverse(unsigned n) if (n 10) cout n endl; else cout n % 10; printReverse(n / 10); 4.Just replace the function definition in 3 with that in Exercise 24.5./*- Driver program to test power function of Exercise 25. Input: Integer exponents and real bases Output: Each real

6、 to that integer power -*/#include using namespace std;double power(double x, int n);/*- Recursively compute integer powers of real numbers - Exer. 25. Precondition: None Postcondition: n-th power of x is returned. -*/int main() double base; int exp; for (;) cout base exp; if (base = 0 & exp = 0) br

7、eak; cout base exp = power(base, exp) endl; /- Definition of power()double power(double x, int n) if (n = 0) return 1; else if (n 0) return power(x, n + 1) / x; else return power(x, n - 1) * x;6.Just replace the function definition in 5 with that in Exercise 26.7-9. /*- Driver program to test the re

8、cursive array reversal, array sum, and array location functions of Exercise 27-29. Input: Integer exponents and real bases Output: Each real to that integer power -*/#include using namespace std;typedef int ElementType;const int CAPACITY = 100;typedef ElementType ArrayTypeCAPACITY;void reverseArray(

9、ArrayType arr, int first, int last);/*- Recursively reverse an array - Exer. 27. Precondition: Array arr has elements in positions first through last. Postcondition: Elements in positions first through last of arr are reversed. -*/ElementType sumArray(ArrayType arr, int n);/*- Recursively sum the el

10、ements of an array - Exer. 28. Precondition: Array arr has n elements. Postcondition: Sum of the elements is returned -*/int location(ArrayType arr, int first, int last, ElementType item);/*- Recursively search the elements of an array - Exer. 28. Precondition: Array arr has elements in positions fi

11、rst through last. Postcondition: Location of item in arr is returned; -1 if item isnt found. -*/void display(ArrayType arr, int n);/*- Display the elements of an array. Precondition: Array arr has n elements. Postcondition: Elements of arr have been output to cout. -*/int main() ArrayType x; cout En

12、ter at most CAPACITY integers (-1 to stop):n; int item, count; for (count = 0; count item; if (item 0) break; xcount = item; cout Original array: ; display(x, count); reverseArray(x, 0, count - 1); cout Reversed array: ; display(x, count); cout nSum of array elements = sumArray(x, count) endl; Eleme

13、ntType toFind; for(;) cout toFind; if (toFind 0) break; cout Location of item = location(x, 0, count - 1, toFind) endl where -1 denotes item note found)n; /- Definition of reverseArray()void reverseArray(ArrayType arr, int first, int last) if (first last) ElementType temp = arrfirst; arrfirst = arrl

14、ast; arrlast = temp; reverseArray(arr, first + 1, last - 1); /- Definition of sumArray()ElementType sumArray(ArrayType A, int n) if (n = 0) return 0; if (n = 1) return A0; else return An - 1 + sumArray(A, n - 1);/- Definition of location()int location(ArrayType arr, int first, int last, ElementType

15、item) if (first = last & item != arrfirst) return -1; else if (item = arrlast) return last; else return location(arr, first, last-1, item);/- Definition of display()void display(ArrayType arr, int n) for (int i = 0; i n; i+) cout arri ; cout endl;10./*- Driver program to test the recursive string re

16、versal function of Exercise 30. Input: A string Output: The reversal of the string -*/#include #include using namespace std;string reverse(string word);/*- Recursively reverse a string - Exer. 30. Precondition: None Postcondition: String word has been reversed. -*/int main() string s; cout Enter a s

17、tring: ; getline(cin, s); cout Original string: s endl; cout Reversed string: reverse(s) endl;/- Definition of reverse()string reverse(string word) if (word.length() = 1) return word; else return reverse(word.substr(1, word.length() - 1) + word0;11.Simply replace the definition of reverse() in Probl

18、em 10 with the nonrecursive version from Exercise 31.12./*- Driver program to test the recursive palindrome checking function of Exercise 32. Input: An integer Output: An indication of whether the integer is a palindrome -*/#include #include using namespace std;bool palindrome (unsigned number, int

19、numDigits);/*- Recursively check if an integer is a palindrome - Exer. 32. Precondition: number has numDigits digits. Postcondition: True is returned if number is a palindrome, and false otherwise. -*/int main() int x, n; cout x n; cout Palindrome? (palindrome(x, n) ? Yes : No) endl;/- Definition of

20、 palindrome()bool palindrome (unsigned number, int numDigits) if (numDigits = 1) return true; else unsigned powerOf10; powerOf10 = (unsigned)pow(10.0, numDigits - 1); unsigned firstDigit = number / powerOf10 ; unsigned lastDigit = number % 10; if (firstDigit != lastDigit) return false; else return p

21、alindrome(number % powerOf10) / 10, numDigits - 2); 13./*- Driver program to test the recursive palindrome checking function of Exercise 32. Input: An integer Output: An indication of whether the integer is a palindrome -*/#include using namespace std;int gcd(int a, int b);/*- Recursively compute gc

22、d of two integers - Exer. 33. Precondition: Not both of a and b are zero. Postcondition: GCD of a and b is returned. -*/int main() int a, b; for (;) cout a b; if (a = 0 & b = 0) break; cout GCD = gcd(a, b) endl; /- Definition of gcd()int gcd(int a, int b) if (a 0) a = -a; if (b 0) b = -b; if (b = 0)

23、 return a; else return gcd(b, a % b);14. Simply replace the recursive function gcd() in Problem 13 with the nonrecursive version from Exercise 34.15./*- Driver program to test the recursive binomial coefficient function of Exercise 35. Input: Pairs of integer Output: The binomial coefficient corresp

24、onding to that pair. -*/#include using namespace std;int recBinomCoef(int n, int k);/*- Recursively compute a binomial coefficient - Exer. 35. Precondition: 0 = k = n & n != 0. Postcondition: The binomial coefficient n above k is returned. -*/int main() int a, b; for (;) cout = second (0 0 to stop):

25、 ; cin a b; if (a = 0 & b = 0) break; if (a = b) cout C( a , b ) = recBinomCoef(a, b) endl; /- Definition of binomCoeff()int recBinomCoef(int n, int k) if (n = k | k = 0) return 1; else return recBinomCoef(n - 1, k - 1) + recBinomCoef(n - 1, k);16./*- Driver program to time the recursive binomial co

26、efficient function of Exercise 35 and the nonrecursive version of Exercise 36. Input: Pairs of integer Output: The binomial coefficient corresponding to that pair and the times to compute it both recursively and nonrecursively. NOTE: Binomial coefficients are being computed as doubles because large

27、ones need to be computed to achieve nonzero times, and integer values will overflow. -*/#include using namespace std;#include Timer.hdouble recBinomCoef(int n, int k);/*- Recursively compute a binomial coefficient - Exer. 35. Precondition: 0 = k = n & n != 0. Postcondition: The binomial coefficient

28、n above k is returned. -*/double nonrecBinomCoef(int n, int k);/*- Iteratively compute a binomial coefficient - Exer. 36. Precondition: 0 = k = n & n != 0. Postcondition: The binomial coefficient n above k is returned. -*/int main() int a, b; for (;) cout = second (0 0 to stop): ; cin a b; if (a = 0

29、 & b = 0) break; if (a = b) double recCoeff, / recursive binom coeff. nonRecCoeff; / use double due to integer overflor Timer tRec, tNonrec; tRec.start(); recCoeff = recBinomCoef(a, b); tRec.stop(); tNonrec.start(); nonRecCoeff = nonrecBinomCoef(a, b); tNonrec.stop(); / Display the times cout C( a , b ) = recCoeff endl; cout * Recursive binom. coeff. took: ; tRec.print(cout); cout endl; cout C( a , b ) = nonRecCoeff endl; cout * Nonrecursive binom. coeff. took: ; tNonrec.print(cout); cout n/2) k = n - k; for (int i = n-1; i = k + 1; i-) num *= i; for (int i = 2; i = n-k; i+) denom *= i;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 财经金融

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁