数据结构与算法分析-第10章-答案--larry-nyhoff-清华大学出版社.docVIP

  • 4
  • 0
  • 约4.72万字
  • 约 42页
  • 2020-06-11 发布于上海
  • 举报

数据结构与算法分析-第10章-答案--larry-nyhoff-清华大学出版社.doc

Chapter 10: ADT Implementations: Recursion, Algorithm Analysis, and Standard Algorithms Programming Problems Section 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 iostream using namespace 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 Enter a nonnegative integer (-1 to stop): ; cin n; if (n 0) break; cout # digits = numDigits(n) endl; } } //-- Definition of numDigits() int numDigits(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 iostream using namespace 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 \nEnter a nonnegative integer (-1 to stop): ; cin n; if (n 0) break; cout Reversal is: ; printReverse(n); } } //-- Definition of

文档评论(0)

1亿VIP精品文档

相关文档