




已阅读5页,还剩38页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 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 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 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 n; if (n 0) break; cout Reversal is: ; printReverse(n); /- Definition 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 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) break; 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 recursive 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(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 elements 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 first 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 Enter 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; ElementType 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 = arrlast; 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 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 reversal 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 string: ; 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 Problem 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 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 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 palindrome(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 gcd 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) 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 corresponding 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): ; 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 coefficient 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 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 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 & 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; return num/denom;17./*- Program to trace recursive calls of a function. Here the function f() of Exercise 13 is used. Input: An integer Output: Trace of recursive calls and returns and the value of f at that integer -*/#include / cin, cout, , using namespace std;void indent(int numSpaces);/*- Indent the output numSpaces spaces from current position. Precondition: None Postcondition: Output has advanced spaces. -*/unsigned f(unsigned n);/*- Modification of function f of Exercise 13 - statements are added to output a trace of the recursive calls to and returns from f(). Precondition: None Postcondition: Trace of recursive calls and returns was output to cout and the value of f returned. -*/int main() int number; cout number; f(number);/- Definition of indent()void indent(int numSpaces) while (numSpaces-) cout ;/- Definition of f()unsigned f(unsigned n) static int level = 0; / static variable to control indentation if (n 2) indent(level); cout f( n ) returns 0n; return 0; else indent(level); cout f( n ) = 1 + f( (n / 2) )n; level+; unsigned value = 1 + f(n / 2); level-; indent(level); cout f( n ) returns value n; return value; 18. /*- Program to trace recursive calls of the function printReverse() of Exercise 23. Input: An integer Output: Trace of recursive calls and returns and the reversal of that integer. -*/#include / cin, cout, , = 0 Postcondition: Reversal of n has been outpu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 聚合反应机理分析报告
- 印刷企业财务成本控制与优化分析报告
- 网络安全防护机制评估报告
- 中医结合试题及答案
- 中医考试题及答案分析
- 中医科技师面试题及答案
- 中医三基基础测试题及答案
- 中医膳食护理试题及答案
- 2025年生态茶园观光旅游项目生态旅游培训与发展建议
- 中医食疗考试题及答案
- 2025年列车长(官方)-高级工历年参考试题库答案解析(5卷套题【单项选择题100题】)
- 高三生物一轮复习课件微专题5电子传递链化学渗透假说及逆境胁迫
- DBJ50-T-306-2024 建设工程档案编制验收标准
- 2025四川雅安荥经县国润排水有限责任公司招聘5人笔试历年参考题库附带答案详解
- 2025中国银行新疆区分行社会招聘笔试备考试题及答案解析
- 动脉置管并发症
- 药品医疗器械试题及答案
- 子宫内膜类器官构建与临床转化专家共识解读 2
- 2025年甘肃省高考历史试卷真题(含答案解析)
- ESD手术常见并发症
- 普通话驾驶员培训课件
评论
0/150
提交评论