數據結構、算法與應用C++語言描述(第二版) 第一章部分練習參考答案

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)[N],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)[N], const T(&b)[N])
{
	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)[N])
{		//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、








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章