- 4
- 0
- 约4.72万字
- 约 42页
- 2020-06-11 发布于上海
- 举报
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
您可能关注的文档
- 时代楷模事迹材料时代楷模高宝来事迹.docx
- 时代理想人生演讲稿.docx
- 时代杂志评出的100部最好英语小说.doc
- 时光作文之最好的时光在路上作文.docx
- 时光作文之在时光里的作文.docx
- 时间管理的本质是“事件”管理.docx
- 时间去哪儿了-阅读理解及答案.docx
- 时间作文之按时间顺序写的作文500字.docx
- 时间作文之论时间重要性八百字作文.docx
- 时间作文之如何在大学利用时间英语作文1.docx
- 伟明环保-市场前景及投资研究报告-境内业务稳健运行,印尼市场贡献边际增量.pdf
- 桂东县法院系统招聘考试真题2025.pdf
- 贵州省黔南布依族2026年中考三模物理试题及答案.pdf
- 贵州省黔南州2026年中考语文二模试卷附答案.pdf
- 贵州省铜仁市2026年中考语文二模试卷附答案.pdf
- 2026上半年安徽事业单位联考合肥市庐江县招聘36人备考题库及一套完整答案详解.docx
- 贵州省毕节市2026年中考语文一模试卷附答案.pdf
- 贵州省贵阳市南明区2026年中考语文一模试卷附答案.pdf
- 2026上半年安徽事业单位联考合肥市庐江县招聘36人备考题库及一套参考答案详解.docx
- 贵州省贵阳市白云区2026年中考二模物理试题附答案.pdf
最近下载
- 人教版八年级历史下册知识点.doc VIP
- 2025年南昌健康职业技术学院单招(语文)测试模拟题库审定版.docx VIP
- ISO 5817-2023 焊接一钢、镰、铁及其合金的熔化焊接头(能量柬焊除外) 缺欠质量等级(中文版).pdf
- 2025年西藏自治区中考化学真题(含答案).pdf
- 【北师大版】五年级上册数学复习测试卷 (2).doc VIP
- 2025年高考真题——地理(湖南卷)含答案 .pdf VIP
- 【北师大版】五年级上册数学期末测试卷(含答案).docx VIP
- 2025年西藏中考道德与法治试卷及答案 .pdf VIP
- 2026年春八年级下册道德与法治全册知识点汇总.docx VIP
- 【北师大版】五年级数学上册《分数加减法》单元测试卷.doc VIP
原创力文档

文档评论(0)