您的位置:首页 > 理论基础 > 数据结构算法

数据结构、算法与应用C++语言描述(第二版) 第一章部分练习参考答案

2018-01-16 14:00 183 查看
1、

void swap(int& x,int& y)
{//交换x,y
int temp=x;
x=y;
y=temp;
}


2、

template<class T,unsigned N> size_t count(const T (&a)
,const T& value)
{
size_t c_num=0;
for(size_t i=0;i<N;i++)		//使用支持c++11的编译器还可以直接使用auto
if(a[i]==value)
c_num++;
return c_num;
}

3、

template <class T>
void fill(T* a, int start, int end, const T& value)
{
for (int i = start; i < end; i++)
a[i] = value;
}

4、
template<class T,unsigned N>
T inner_product(const T(&a)
, const T(&b)
)
{
T sum = 0;
for (size_t i = 0;i < N;i++)
sum += a[i] * b[i];
return sum;
}

5、

template <class T>
void iota(T* a, int n, const T& value)
{
for (int i = 0; i < n; i++)
a[i] += value;
}

6、
template<class T,unsigned N>
bool is_sorted(const T(&a)
)
{		//C++里面,标准库判断有序常用的序列为<,这里也只判断是否为<
for (size_t i = 0;i < N - 1;i++)
if (a[i] < a[i + 1])
continue;
else
return false;
return true;
}

7、
template <class T>
int mismatch(T* a, T* b, int n)
{
for (int i = 0; i < n; i++)
if (a[i] != b[i])
return i;
return n;
}

8、一个函数的签名是由这个函数的形参类型及形参个数确定的。两个签名都是(int,int,int)
9、

(1)调用int版本

(2)调用float版本

(3)报错:两个重载有相似的转换;

(4)报错:对重载函数的调用不明确。这里1.0,2.0,3.0的类型是double

10、

int abc(int a, int b, int c)
{
if (a < 0 && b < 0 & c < 0)
throw 1;
else if (a == 0 && b == 0 && c == 0)
throw 2;
return a + b*c;
}

11、
template <class T>
int count(T a[], int n, const T& value)
{
if (n < 1)
throw "n must be >= 1";
int theCount = 0;
for (int i = 0; i < n; i++)
if (a[i] == value)
theCount++;
return theCount;
}

12、

template <class T>
void make2darray(T** &x, int numberofRows, int* rowSize)
{
x = new T*[numberofRows];
for (int i = 0;i < numberofRows;i++)
x[i] = new T[rowSize[i]];
}

13、

template<class T>
T* changeLength1D(T* &arr, int newLength)
{//arr必须是new分配的动态内存,否则无法delete删除
int oldLength = sizeof(arr);
T* newarr = new T[newLength];
for (int i = 0;i < (oldLength < newLength ? oldLength : newLength);i++)
newarr[i] = arr[i];
delete[] arr;
arr = nullptr;
return newarr;
}

14、

template<class T>
T** changeLength2D(T** arr, int oldrow, int oldcolumn,int newrow,int newcolumn)
{
T** newarr = new T*[newrow];
for (int i = 0;i < newrow;i++)
{
newarr[i] = new T[newcolumn];
for (int j = 0;j < newcolumn;j++)
newarr[i][j] = arr[(i*newcolumn+j)/oldcolumn][(i*newcolumn + j) % oldcolumn];
}
for (int i = 0;i < oldrow;i++)			//删除原数组
delete[] arr[i];
delete[] arr;
arr = nullptr;
return newarr;
}

15、

(1)最大值:2^32-1 dollars and 99 cents    最小值:-2^32-1 dollars and 99 cents

(2)最大值:2^31-1 dollars and 99 cents 最小值:-2^31-1 dollars and 99 cents

(3)不超过 MAXVALUE/100

16、
void input()
{
// input the amount as a double
cout << "Enter the currency amount with its sign dollars and cents" << endl;
signType thesign;
unsigned long thedollars;
unsigned int thecents;
cin >> thesign>>thedollars>>thecents;
// set the value
setValue(thesign,thedollars,thecents);
}

currency subtract(const currency& x)
{// Return *this - x.
currency result;
long a1,a2,a3;
a1=dollars*100+cents;
if(sign==minus) a1=-a1;
a2=x.dollars*100+x.cents;
if(x.sign==minus) a2=-a2;
a3=a1-a2;
if(a3<0){result.sign=minus;a3=-a3;}
else result.sign=plus;
result.dollars=a3/100;
result.cents=a3-result.dollars*100;
return result;
}

currency percent(double x)
{// Return x percent of *this.
currency result;
long a1,a2;
a1=dollars*100+cents;
a2=(long)(a1*x/100);
result.sign=sign;
result.dollars=a2/100;
result.cents=a2-result.dollars*100;
return result;
}

currency multiply(double x)
{// Return this * x.
currency result;
long a1,a2;
a1=dollars*100+cents;
a2=a1*x;
result.sign=sign;
result.dollars=(long)a2/100;
result.cents=a2-result.dollars*100;
return result;
}

currency divide(double x)
{// Return this / x.
currency result;
long a1,a2;
a1=dollars*100+cents;
a2=a1/x;
result.sign=sign;
result.dollars=(long)a2/100;
result.cents=a2-result.dollars*100;
return result;
}


17、

void input()
{
// input the amount as a double
cout << "Enter the currency amount as a real number" << endl;
double theValue;
cin >> theValue;

// set the value
setValue(theValue);
}

currency subtract(const currency& x)
{// Return *this - x.
currency result;
result.amount = amount - x.amount;
return result;
}

currency percent(float x)
{// Return x percent of *this.
currency result;
result.amount = (long) (amount * x / 100);
return result;
}

currency multiply(float x)
{// Return this * x.
currency result;
result.amount = (long) (amount * x);
return result;
}

currency divide(float x)
{// Return this / x.
currency result;
result.amount = (long) (amount / x);
return result;
}

18、略
19、

int factorial(int n)
{
if(n<=1)
return 1;
else
{
int fac=1;
for(int i=1;i<=n;i++)
fac*=i;
return fac;
}
}

20、
(1)

int Fibonacci(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return Fibonacci(n-1)+Fibonacci(n-2);
}

(2)略
(3)

int Fibonacci(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
{
int fib=0;
int temp1=0,temp2=1;
for(int i=2;i<=n;i++)
{
fib=temp1+temp2;
temp1=temp2;
temp2=fib;
}
return fib;
}
}

21、
(3)

int f(int n)
{
if(n%2==0)
return n/2;
else
return f(3*n+1);
}

(4)

int f(int n)
{
if(n%2==0)
return n/2;
else
return (3*(2*(n/2)+1)+1)/2;
}

22、(3)

int Ackermann(int i,int j)
{
if(i==1&&j>=1)
return pow(2,double(j));
else if(i==2&&j==1)
return Ackermann(i-1,2);
else if(i>=2&&j>=2)
return Ackermann(i-1,Ackermann(i,j-1));
}

23、

int gcd(int x,int y)
{
//为方便期间,调整顺序到x>=y
if(x<y)
{
int temp=x;
x=y;
y=temp;
}
if(y==0)
return x;
else
return gcd(y,x%y);
}

24、略

25、template <typename T>
void Sub(vector<T> tv, int n, vector<int> tag) {
if (n == 0) {
for (int i = 0; i < tag.size(); i++)
cout << tag[i];
cout<<endl;
}
else
{
tag[n-1]=0;
Sub(tv,n-1,tag);
tag[n-1]=1;
Sub(tv,n-1,tag);

}
} 26、
void g(int n)
{
if(n==1)
cout<<1;
else
{
g(n-1);
cout<<n;
g(n-1);
}
}27、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: