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