数据结构与算法分析 第10章 答案Larry Nyhoff 清华大学出版社.docx
- 文档编号:5914075
- 上传时间:2023-01-02
- 格式:DOCX
- 页数:59
- 大小:29.06KB
数据结构与算法分析 第10章 答案Larry Nyhoff 清华大学出版社.docx
《数据结构与算法分析 第10章 答案Larry Nyhoff 清华大学出版社.docx》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第10章 答案Larry Nyhoff 清华大学出版社.docx(59页珍藏版)》请在冰豆网上搜索。
数据结构与算法分析第10章答案LarryNyhoff清华大学出版社
Chapter10:
ADTImplementations:
Recursion,AlgorithmAnalysis,andStandardAlgorithms
ProgrammingProblems
Section10.1
1.
/*-------------------------------------------------------------------
Driverprogramtotestrecursivedigit-countingfunctionof
Exercise21.
Input:
Nonnegativeintegers
Output:
Numberofdigitsineachinteger
-------------------------------------------------------------------*/
#include
usingnamespacestd;
intnumDigits(unsignedn);
/*-------------------------------------------------------------------
Recursivelycountthedigitsinanonnegativeinteger--Exer.21.
Precondition:
n>=0
Postcondition:
Numberofdigitsinnisreturned.
-------------------------------------------------------------------*/
intmain()
{
intn;
for(;;)
{
cout<<"Enteranonnegativeinteger(-1tostop):
";
cin>>n;
if(n<0)break;
cout<<"#digits="< } } //--DefinitionofnumDigits() intnumDigits(unsignedn) { if(n<10) return1; else return1+numDigits(n/10); } 2.Justreplacethefunctiondefinitionin1withthatinExercise22. 3. /*------------------------------------------------------------------- Driverprogramtotestreverse-printfunctionofExercise23. Input: Nonnegativeintegers Output: Thereversalofeachinteger. -------------------------------------------------------------------*/ #include usingnamespacestd; voidprintReverse(unsignedn); /*------------------------------------------------------------------- Recursivelydisplaythedigitsofanonnegativeintegerinreverse order--Exer.23. Precondition: n>=0 Postcondition: Reversalofnhasbeenoutputtocout. -------------------------------------------------------------------*/ intmain() { intn; for(;;) { cout<<"\nEnteranonnegativeinteger(-1tostop): "; cin>>n; if(n<0)break; cout<<"Reversalis: "; printReverse(n); } } //--DefinitionofprintReverse() voidprintReverse(unsignedn) { if(n<10) cout< else { cout< printReverse(n/10); } } 4.Justreplacethefunctiondefinitionin3withthatinExercise24. 5. /*------------------------------------------------------------------- DriverprogramtotestpowerfunctionofExercise25. Input: Integerexponentsandrealbases Output: Eachrealtothatintegerpower -------------------------------------------------------------------*/ #include usingnamespacestd; doublepower(doublex,intn); /*------------------------------------------------------------------- Recursivelycomputeintegerpowersofrealnumbers--Exer.25. Precondition: None Postcondition: n-thpowerofxisreturned. -------------------------------------------------------------------*/ intmain() { doublebase; intexp; for(;;) { cout<<"\nEnterarealbaseandanintegerexponent(00tostop): "; cin>>base>>exp; if(base==0&&exp==0)break; cout< } } //--Definitionofpower() doublepower(doublex,intn) { if(n==0) return1; elseif(n<0) returnpower(x,n+1)/x; else returnpower(x,n-1)*x; } 6.Justreplacethefunctiondefinitionin5withthatinExercise26. 7-9. /*------------------------------------------------------------------- Driverprogramtotesttherecursivearrayreversal,arraysum, andarraylocationfunctionsofExercise27-29. Input: Integerexponentsandrealbases Output: Eachrealtothatintegerpower -------------------------------------------------------------------*/ #include usingnamespacestd; typedefintElementType; constintCAPACITY=100; typedefElementTypeArrayType[CAPACITY]; voidreverseArray(ArrayTypearr,intfirst,intlast); /*------------------------------------------------------------------- Recursivelyreverseanarray--Exer.27. Precondition: Arrayarrhaselementsinpositionsfirstthrough last. Postcondition: Elementsinpositionsfirstthroughlastofarr arereversed. -------------------------------------------------------------------*/ ElementTypesumArray(ArrayTypearr,intn); /*------------------------------------------------------------------- Recursivelysumtheelementsofanarray--Exer.28. Precondition: Arrayarrhasnelements. Postcondition: Sumoftheelementsisreturned -------------------------------------------------------------------*/ intlocation(ArrayTypearr,intfirst,intlast,ElementTypeitem); /*------------------------------------------------------------------- Recursivelysearchtheelementsofanarray--Exer.28. Precondition: Arrayarrhaselementsinpositionsfirstthrough last. Postcondition: Locationofiteminarrisreturned;-1ifitem isn'tfound. -------------------------------------------------------------------*/ voiddisplay(ArrayTypearr,intn); /*------------------------------------------------------------------- Displaytheelementsofanarray. Precondition: Arrayarrhasnelements. Postcondition: Elementsofarrhavebeenoutputtocout. -------------------------------------------------------------------*/ intmain() { ArrayTypex; cout<<"Enteratmost"< \n"; intitem,count; for(count=0;count { cin>>item; if(item<0)break; x[count]=item; } cout<<"Originalarray: "; display(x,count); reverseArray(x,0,count-1); cout<<"Reversedarray: "; display(x,count); cout<<"\nSumofarrayelements="< ElementTypetoFind; for(;;) { cout<<"Enteranitemtosearchfor(-1tostop): "; cin>>toFind; if(toFind<0)break; cout<<"Locationofitem="< < } } //--DefinitionofreverseArray() voidreverseArray(ArrayTypearr,intfirst,intlast) { if(first { ElementTypetemp=arr[first]; arr[first]=arr[last]; arr[last]=temp; reverseArray(arr,first+1,last-1); } } //--DefinitionofsumArray() ElementTypesumArray(ArrayTypeA,intn) { if(n==0) return0; if(n==1) returnA[0]; else returnA[n-1]+sumArray(A,n-1); } //--Definitionoflocation() intlocation(ArrayTypearr,intfirst,intlast,ElementTypeitem) { if(first==last&&item! =arr[first]) return-1; elseif(item==arr[last]) returnlast; else returnlocation(arr,first,last-1,item); } //--Definitionofdisplay() voiddisplay(ArrayTypearr,intn) { for(inti=0;i cout< cout< } 10. /*------------------------------------------------------------------- Driverprogramtotesttherecursivestringreversalfunctionof Exercise30. Input: Astring Output: Thereversalofthestring -------------------------------------------------------------------*/ #include #include usingnamespacestd; stringreverse(stringword); /*------------------------------------------------------------------- Recursivelyreverseastring--Exer.30. Precondition: None Postcondition: Stringwordhasbeenreversed. -------------------------------------------------------------------*/ intmain() { strings; cout<<"Enterastring: "; getline(cin,s); cout<<"Originalstring: "< cout<<"Reversedstring: "< } //--Definitionofreverse() stringreverse(stringword) { if(word.length()==1) returnword; else returnreverse(word.substr(1,word.length()-1))+word[0]; } 11.Simplyreplacethedefinitionofreverse()inProblem10withthenonrecursiveversionfromExercise31. 12. /*------------------------------------------------------------------- Driverprogramtotesttherecursivepalindromecheckingfunction ofExercise32. Input: Aninteger Output: Anindicationofwhethertheintegerisapalindrome -------------------------------------------------------------------*/ #include #include usingnamespacestd; boolpalindrome(unsignednumber,intnumDigits); /*------------------------------------------------------------------- Recursivelycheckifanintegerisapalindrome--Exer.32. Precondition: numberhasnumDigitsdigits. Postcondition: Trueisreturnedifnumberisapalindrome,and falseotherwise. -------------------------------------------------------------------*/ intmain() { intx,n; cout<<"Enterinteger&numberofdigits: "; cin>>x>>n; cout<<"Palindrome? "<<(palindrome(x,n)? "Yes": "No")< } //--Definitionofpalindrome() boolpalindrome(unsignednumber,intnumDigits) { if(numDigits<=1) returntrue; else { unsignedpowerOf10; powerOf10=(unsigned)pow(10.0,numDigits-1); unsignedfirstDigit=number/powerOf10; unsignedlastDigit=number%10; if(firstDigit! =lastDigit) returnfalse; else returnpalindrome((number%powerOf10)/10,numDigits-2); } } 13. /*------------------------------------------------------------------- Driverprogramtotesttherecursivepalindromecheckingfunction ofExercise32. Input: Aninteger Output: Anindicationofwhethertheintegerisapalindrome -------------------------------------------------------------------*/ #include usingnamespacestd; intgcd(inta,intb); /*------------------------------------------------------------------- Recursivelycomputegcdoftwointegers--Exer.33. Precondition: Notbothofaandbarezero. Postcondition: GCDofaandbisreturned. -----------------
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析 第10章 答案 Larry Nyhoff 清华大学出版社 数据结构 算法 分析 10