C++ Exercises(六)
《数据结构算法与应用--C++语言描述》第一章习题解答
Code
1.1
#include <iostream>
using namespace std;
const int MAX_TRY = 3;
template <class T>
bool Input(T& data)
{
int n_trys = 0;
do
{
cout<<"请输入一个数: ";
cin>>data;
if(data>=0)
{
return true;
}
else
{
cout<<"输入非法,请重新输入."<<endl;
n_trys++;
}
}while(n_trys<MAX_TRY);
return false;
}
int main(void)
{
double num;
if(Input(num))
{
cout<<"你输入的数是: "<<num<<endl;
}
else
{
cout<<"已经输入次了,你失败了"<<endl;
}
cin>>num;
return 0;
}
1.2
#include <iostream>
using namespace std;
template <class T>
bool IsAsscendArray(const T* a,int n)
{//测试数组是否升序排列
bool isAsscend = true;
for(int i=0;i<n;++i)
{
if(a[i]>a[i+1])
{
isAsscend = false;
}
}
return isAsscend;
}
int main(void)
{
int a[] = {1,0,3,4},tmp;
cout<<IsAsscendArray(a,4)<<endl;
cin>>tmp;
return 0;
}
1.3
#include <iostream>
using namespace std;
long double jiecheng(int n)
{
long double result = n;
for(int i=n-1;i>=1;--i)
{
result*=i;
}
return result;
}
int main(void)
{
int tmp,num;
long double result = 0;
cout<<"输入阶层数: ";
cin>>num;
cout<<"结果: "<<jiecheng(num)<<endl;
cin>>tmp;
return 0;
}
1.4
#include <iostream>
using namespace std;
long fibo(int n)
{
if(n==1||n==2)
{
return 1;
}
else
{
return fibo(n-1)+fibo(n-2);
}
}
long fibo2(int n)
{
int a1 = 1,a2 = 1,tmp;
if(n==1||n==2)
return 1;
else
{
for(int i= 3;i<=n;++i)
{
tmp = a1+a2;
a1 = a2;
a2 = tmp;
}
return tmp;
}
}
int main(void)
{
int tmp;
int num;
cout<<"输入fibo数: ";
cin>>num;
cout<<"结果: "<<fibo2(num)<<endl;
cin>>tmp;
return 0;
}
1.5,求子集合
#include <iostream>
using namespace std;
char a[] = {'a','b','c','d'};
const int MAX = sizeof(a)/sizeof(char);
bool used[MAX]={false},first = false;
void sub(int n)
{
if(n==MAX)
{
if(first==false)
{
cout<<"空集"<<endl;
first = true;
return;
}
for(int i=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);
}
}
int main()
{
sub(0);//从第一个元素开始挑选
return 0;
}
1.6
#include <iostream>
using namespace std;
int a[] = {1,2,3,4};
const int size = sizeof(a)/sizeof(int);
bool contain(int a[],int num,int n)
{
if(n==0)
{
if(num==a[n])
return true;
else
return false;
}
else
{
if(a[n-1]==num)
return true;
else
return contain(a,num,n-1);
}
}
int main()
{
int m;
do
{
cout<<"输入一个数:";
cin>>m;
cout<<"结果是:"<<contain(a,m,size)<<endl;
} while (m!=-1);
return 0;
}
1.3节示例
#include <iostream>
using namespace std;
template <class T>
bool Make2DArray(T **&x,int rows,int cols)
{
try
{
x = new T*[rows];
for(int i=0;i<rows;++i)
{
x[i] = new T[cols];
}
return true;
}
catch (string ex)
{
cout<<"error occur when malloc"<<endl;
return false;
}
}
template <class T>
void DeleteArray(T** &x,int rows)
{
cout<<"enter delete array";
for (int i=0;i<rows;++i)
{
delete [] x[i];
}
delete [] x;
}
template <class T>
void Display(T** &x,int rows,int cols)
{
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cout<<x[i][j]<<'\t';
}
cout<<endl;
}
}
template <class T>
void EnterData(T** &x,int rows,int cols)
{
cout<<"请输入数据:"<<endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
int main(void)
{
int rows,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;
return 0;
}
1.7
template <class T>
void ChangeSize1D(T *&a,int size,T *&b,int toSize)
{//将数组a的大小从size变成toSize
b = new T[toSize];
for (int i=0;i<toSize;i++)
{
b[i] = a[i];
}
delete [] a;
}
int main(void)
{
int n;
int *a = new int[4];
for(int i=0;i<4;i++)
{
a[i] = i;
}
int *b;
ChangeSize1D(a,4,b,3);
int size = sizeof(b)/sizeof(int);
for (int i=0;i<3;i++)
{
cout<<b[i];
}
cout<<endl;
cin>>n;
return 0;
}
1.8
#include <iostream>
using namespace std;
template <class T>
bool Make2DArray(T **&x,int rows,int cols)
{
try
{
x = new T*[rows];
for(int i=0;i<rows;++i)
{
x[i] = new T[cols];
}
return true;
}
catch (string ex)
{
cout<<"error occur when malloc"<<endl;
return false;
}
}
template <class T>
void DeleteArray(T** &x,int rows)
{
cout<<"enter delete array\n";
for (int i=0;i<rows;++i)
{
delete [] x[i];
}
delete [] x;
}
template <class T>
void Display(T** &x,int rows,int cols)
{
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cout<<x[i][j]<<'\t';
}
cout<<endl;
}
}
template <class T>
void EnterData(T** &x,int rows,int cols)
{
cout<<"请输入数据:"<<endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
template <class T>
void ChangeSize2D(T** &a,int oldRows,int oldCols,T** &b,int newRows,int newCols)
{
Make2DArray(b,newRows,newCols);//构造新的数组
for (int i=0;i<newRows;++i)
{
for (int j=0;j<newCols;++j)
{
b[i][j] = a[i][j];
}
}
DeleteArray(a,oldRows);//删除旧数组
}
int main(void)
{
int n;
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;
return 0;
}
1.1
#include <iostream>
using namespace std;
const int MAX_TRY = 3;
template <class T>
bool Input(T& data)
{
int n_trys = 0;
do
{
cout<<"请输入一个数: ";
cin>>data;
if(data>=0)
{
return true;
}
else
{
cout<<"输入非法,请重新输入."<<endl;
n_trys++;
}
}while(n_trys<MAX_TRY);
return false;
}
int main(void)
{
double num;
if(Input(num))
{
cout<<"你输入的数是: "<<num<<endl;
}
else
{
cout<<"已经输入次了,你失败了"<<endl;
}
cin>>num;
return 0;
}
1.2
#include <iostream>
using namespace std;
template <class T>
bool IsAsscendArray(const T* a,int n)
{//测试数组是否升序排列
bool isAsscend = true;
for(int i=0;i<n;++i)
{
if(a[i]>a[i+1])
{
isAsscend = false;
}
}
return isAsscend;
}
int main(void)
{
int a[] = {1,0,3,4},tmp;
cout<<IsAsscendArray(a,4)<<endl;
cin>>tmp;
return 0;
}
1.3
#include <iostream>
using namespace std;
long double jiecheng(int n)
{
long double result = n;
for(int i=n-1;i>=1;--i)
{
result*=i;
}
return result;
}
int main(void)
{
int tmp,num;
long double result = 0;
cout<<"输入阶层数: ";
cin>>num;
cout<<"结果: "<<jiecheng(num)<<endl;
cin>>tmp;
return 0;
}
1.4
#include <iostream>
using namespace std;
long fibo(int n)
{
if(n==1||n==2)
{
return 1;
}
else
{
return fibo(n-1)+fibo(n-2);
}
}
long fibo2(int n)
{
int a1 = 1,a2 = 1,tmp;
if(n==1||n==2)
return 1;
else
{
for(int i= 3;i<=n;++i)
{
tmp = a1+a2;
a1 = a2;
a2 = tmp;
}
return tmp;
}
}
int main(void)
{
int tmp;
int num;
cout<<"输入fibo数: ";
cin>>num;
cout<<"结果: "<<fibo2(num)<<endl;
cin>>tmp;
return 0;
}
1.5,求子集合
#include <iostream>
using namespace std;
char a[] = {'a','b','c','d'};
const int MAX = sizeof(a)/sizeof(char);
bool used[MAX]={false},first = false;
void sub(int n)
{
if(n==MAX)
{
if(first==false)
{
cout<<"空集"<<endl;
first = true;
return;
}
for(int i=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);
}
}
int main()
{
sub(0);//从第一个元素开始挑选
return 0;
}
1.6
#include <iostream>
using namespace std;
int a[] = {1,2,3,4};
const int size = sizeof(a)/sizeof(int);
bool contain(int a[],int num,int n)
{
if(n==0)
{
if(num==a[n])
return true;
else
return false;
}
else
{
if(a[n-1]==num)
return true;
else
return contain(a,num,n-1);
}
}
int main()
{
int m;
do
{
cout<<"输入一个数:";
cin>>m;
cout<<"结果是:"<<contain(a,m,size)<<endl;
} while (m!=-1);
return 0;
}
1.3节示例
#include <iostream>
using namespace std;
template <class T>
bool Make2DArray(T **&x,int rows,int cols)
{
try
{
x = new T*[rows];
for(int i=0;i<rows;++i)
{
x[i] = new T[cols];
}
return true;
}
catch (string ex)
{
cout<<"error occur when malloc"<<endl;
return false;
}
}
template <class T>
void DeleteArray(T** &x,int rows)
{
cout<<"enter delete array";
for (int i=0;i<rows;++i)
{
delete [] x[i];
}
delete [] x;
}
template <class T>
void Display(T** &x,int rows,int cols)
{
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cout<<x[i][j]<<'\t';
}
cout<<endl;
}
}
template <class T>
void EnterData(T** &x,int rows,int cols)
{
cout<<"请输入数据:"<<endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
int main(void)
{
int rows,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;
return 0;
}
1.7
template <class T>
void ChangeSize1D(T *&a,int size,T *&b,int toSize)
{//将数组a的大小从size变成toSize
b = new T[toSize];
for (int i=0;i<toSize;i++)
{
b[i] = a[i];
}
delete [] a;
}
int main(void)
{
int n;
int *a = new int[4];
for(int i=0;i<4;i++)
{
a[i] = i;
}
int *b;
ChangeSize1D(a,4,b,3);
int size = sizeof(b)/sizeof(int);
for (int i=0;i<3;i++)
{
cout<<b[i];
}
cout<<endl;
cin>>n;
return 0;
}
1.8
#include <iostream>
using namespace std;
template <class T>
bool Make2DArray(T **&x,int rows,int cols)
{
try
{
x = new T*[rows];
for(int i=0;i<rows;++i)
{
x[i] = new T[cols];
}
return true;
}
catch (string ex)
{
cout<<"error occur when malloc"<<endl;
return false;
}
}
template <class T>
void DeleteArray(T** &x,int rows)
{
cout<<"enter delete array\n";
for (int i=0;i<rows;++i)
{
delete [] x[i];
}
delete [] x;
}
template <class T>
void Display(T** &x,int rows,int cols)
{
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cout<<x[i][j]<<'\t';
}
cout<<endl;
}
}
template <class T>
void EnterData(T** &x,int rows,int cols)
{
cout<<"请输入数据:"<<endl;
for (int i=0;i<rows;++i)
{
for (int j=0;j<cols;++j)
{
cin>>x[i][j];
}
}
}
template <class T>
void ChangeSize2D(T** &a,int oldRows,int oldCols,T** &b,int newRows,int newCols)
{
Make2DArray(b,newRows,newCols);//构造新的数组
for (int i=0;i<newRows;++i)
{
for (int j=0;j<newCols;++j)
{
b[i][j] = a[i][j];
}
}
DeleteArray(a,oldRows);//删除旧数组
}
int main(void)
{
int n;
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;
return 0;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-11-19 16:20 Phinecos(洞庭散人) 阅读(524) 评论(0) 编辑 收藏 举报