《数据结构算法与应用--C++语言描述》第一章习题解答
Code
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1.1
#include<iostream>
usingnamespacestd;
constintMAX_TRY=3;
template<classT>
boolInput(T&data)
{
intn_trys=0;
do
{
cout<<"请输入一个数:";
cin>>data;
if(data>=0)
{
returntrue;
}
else
{
cout<<"输入非法,请重新输入."<<endl;
n_trys++;
}
}while(n_trys<MAX_TRY);
returnfalse;
}
intmain(void)
{
doublenum;
if(Input(num))
{
cout<<"你输入的数是:"<<num<<endl;
}
else
{
cout<<"已经输入次了,你失败了"<<endl;
}
cin>>num;
return0;
}
1.2
#include<iostream>
usingnamespacestd;
template<classT>
boolIsAsscendArray(constT*a,intn)
{//测试数组是否升序排列
boolisAsscend=true;
for(inti=0;i<n;++i)
{
if(a[i]>a[i+1])
{
isAsscend=false;
}
}
returnisAsscend;
}
intmain(void)
{
inta[]={1,0,3,4},tmp;
cout<<IsAsscendArray(a,4)<<endl;
cin>>tmp;
return0;
}
1.3
#include<iostream>
usingnamespacestd;
longdoublejiecheng(intn)
{
longdoubleresult=n;
for(inti=n-1;i>=1;--i)
{
result*=i;
}
returnresult;
}
intmain(void)
{
inttmp,num;
longdoubleresult=0;
cout<<"输入阶层数:";
cin>>num;
cout<<"结果:"<<jiecheng(num)<<endl;
cin>>tmp;
return0;
}
1.4
#include<iostream>
usingnamespacestd;
longfibo(intn)
{
if(n==1||n==2)
{
return1;
}
else
{
returnfibo(n-1)+fibo(n-2);
}
}
longfibo2(intn)
{
inta1=1,a2=1,tmp;
if(n==1||n==2)
return1;
else
{
for(inti=3;i<=n;++i)
{
tmp=a1+a2;
a1=a2;
a2=tmp;
}
returntmp;
}
}
intmain(void)
{
inttmp;
intnum;
cout<<"输入fibo数:";
cin>>num;
cout<<"结果:"<<fibo2(num)<<endl;
cin>>tmp;
return0;
}
1.5,求子集合
#include<iostream>
usingnamespacestd;
chara[]={'a','b','c','d'};
constintMAX=sizeof(a)/sizeof(char);
boolused[MAX]={false},first=false;
voidsub(intn)
{
if(n==MAX)
{
if(first==false)
{
cout<<"空集"<<endl;
first=true;
return;
}
for(inti=0;i<MAX;++i)
{
if(used[i]==true)
{
cout<<a[i];
}
}
cout<<endl;
return;
}
else
{
used[n]=false;//不选中
sub(n+1);
used[n]=true;//选中
sub(n+1);
}
}
intmain()
{
sub(0);//从第一个元素开始挑选
return0;
}
1.6
#include<iostream>
usingnamespacestd;
inta[]={1,2,3,4};
constintsize=sizeof(a)/sizeof(int);
boolcontain(inta[],intnum,intn)
{
if(n==0)
{
if(num==a[n])
returntrue;
else
returnfalse;
}
else
{
if(a[n-1]==num)
returntrue;
else
returncontain(a,num,n-1);
}
}
intmain()
{
intm;
do
{
cout<<"输入一个数:";
cin>>m;
cout<<"结果是:"<<contain(a,m,size)<<endl;
}while(m!=-1);
return0;
}
1.3节示例
#include<iostream>
usingnamespacestd;
template<classT>
boolMake2DArray(T**&x,introws,intcols)
{
try
{
x=newT*[rows];
for(inti=0;i<rows;++i)
{
x[i]=newT[cols];
}
returntrue;
}
catch(stringex)
{
cout<<"erroroccurwhenmalloc"<<endl;
returnfalse;
}
}
template<classT>
voidDeleteArray(T**&x,introws)
{
cout<<"enterdeletearray";
for(inti=0;i<rows;++i)
{
delete[]x[i];
}
delete[]x;
}
template<classT>
voidDisplay(T**&x,introws,intcols)
{
for(inti=0;i<rows;++i)
{
for(intj=0;j<cols;++j)
{
cout<<x[i][j]<<'/t';
}
cout<<endl;
}
}
template<classT>
voidEnterData(T**&x,introws,intcols)
{
cout<<"请输入数据:"<<endl;
for(inti=0;i<rows;++i)
{
for(intj=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
intmain(void)
{
introws,cols;
int**a;
cout<<"输入行数和列数:";
cin>>rows>>cols;
if(Make2DArray(a,rows,cols))
{
EnterData(a,rows,cols);
}
Display(a,rows,cols);
DeleteArray(a,rows);
cout<<endl;
return0;
}
1.7
template<classT>
voidChangeSize1D(T*&a,intsize,T*&b,inttoSize)
{//将数组a的大小从size变成toSize
b=newT[toSize];
for(inti=0;i<toSize;i++)
{
b[i]=a[i];
}
delete[]a;
}
intmain(void)
{
intn;
int*a=newint[4];
for(inti=0;i<4;i++)
{
a[i]=i;
}
int*b;
ChangeSize1D(a,4,b,3);
intsize=sizeof(b)/sizeof(int);
for(inti=0;i<3;i++)
{
cout<<b[i];
}
cout<<endl;
cin>>n;
return0;
}
1.8
#include<iostream>
usingnamespacestd;
template<classT>
boolMake2DArray(T**&x,introws,intcols)
{
try
{
x=newT*[rows];
for(inti=0;i<rows;++i)
{
x[i]=newT[cols];
}
returntrue;
}
catch(stringex)
{
cout<<"erroroccurwhenmalloc"<<endl;
returnfalse;
}
}
template<classT>
voidDeleteArray(T**&x,introws)
{
cout<<"enterdeletearray/n";
for(inti=0;i<rows;++i)
{
delete[]x[i];
}
delete[]x;
}
template<classT>
voidDisplay(T**&x,introws,intcols)
{
for(inti=0;i<rows;++i)
{
for(intj=0;j<cols;++j)
{
cout<<x[i][j]<<'/t';
}
cout<<endl;
}
}
template<classT>
voidEnterData(T**&x,introws,intcols)
{
cout<<"请输入数据:"<<endl;
for(inti=0;i<rows;++i)
{
for(intj=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
template<classT>
voidChangeSize2D(T**&a,intoldRows,intoldCols,T**&b,intnewRows,intnewCols)
{
Make2DArray(b,newRows,newCols);//构造新的数组
for(inti=0;i<newRows;++i)
{
for(intj=0;j<newCols;++j)
{
b[i][j]=a[i][j];
}
}
DeleteArray(a,oldRows);//删除旧数组
}
intmain(void)
{
intn;
int**a,**b;
Make2DArray(a,3,3);
EnterData(a,3,3);
Display(a,3,3);
ChangeSize2D(a,3,3,b,2,2);
Display(b,2,2);
cin>>n;
return0;
}
分享到:
相关推荐
C++ PRIMER EXERCISES
C++的一些练习题,《C++语言程序设计(第四版)
Templates: The Hard Way Templates: The C++ Way Function Specialization Class Templates Class Specialization Implementation Details Advanced Features Summary Programming Exercises 25. Standard ...
The C++ Programming Language(4th)Exercises 英文无水印pdf 第4版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如...
C, C++ and Python Code for Exercises and Solutions.zip
C++类的练习(C + + class exercises)
1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3 C++ Terminology 4 ...ANSWERS TO SELF-TEST EXERCISES 39 PROGRAMMING PROJECTS 41
more examples of c++ exercises
Starting out with C++,英文原版,适合初学者
Learn the Standard Template Library and the containers and algorithms used in most real-world C++ applications * Learn how to build effective programs in C++ with hands-on exercises * Get expert tips ...
Mathematical Objects in C++: Computational Tools in A Unified Object-Oriented Approach (Chapman & Hall/CRC Numerical Analysis and Scientific Computing Series) By 作者: Yair Shapira ISBN-10 书号: ...
Author: Josh Lospinoso Pub Date: 2019 ISBN: 978-1593278885 ...With well over 500 code samples and nearly 100 exercises, C++ Crash Course is sure to help you build a strong C++ foundation.
You’ll hone real-world skills through practical exercises, and deepen your expertise through start-to-finish projects that grow in complexity as you build your skills. Throughout, Madhav pays ...
sections in each chapter and a multitude of new and updated exercises. All syntax is explained thoroughly and reinforced through extensive examples and diagrams, and each chapter is full of helpful ...
Syntax examples are clearly marked for handy reference. <br>To help you become more proficient, each lesson ends with a set of common questions and answers, exercises, and a quiz. You can check ...
Features numerous working examples and exercises that help build programming skills Programmer Forums Join our Programmer to Programmer forums to ask and answer programming questions about this book,...
In addition, it now includes * Even more figures, examples, and exercises * Even more puzzles and games * An expanded 75-page language reference * Instructions for downloading free C++ software ...
全英文的哦。。还可以练英语 1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3...ANSWERS TO SELF-TEST EXERCISES 39 PROGRAMMING PROJECTS 41