1. 程式人生 > >計算機程式設計C++(第6周基礎練習)

計算機程式設計C++(第6周基礎練習)

計算機程式設計C++ MOOC

測試與作業C++基礎練習100題

##第六週基本練習

本週內容為函式的遞迴以及過載

  1. 遞迴計算n!
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int fac(int n)
{
	if (n == 0 || n == 1) return 1;
	else return n*fac(n - 1);
}

int main()
{
	int n;
	cin >> n;
	cout << fac(n) << endl;
	return 0;
}
  1. 遞迴計算1+2+3+…+n
    在這裡插入圖片描述
#include "iostream"

using namespace std;

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

int main()
{
	int n;
	cin >> n;
	cout << sum(n) << endl;
	return 0;
}
  1. 遞迴求陣列元素的最大值
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int max(int a[], int n)
{
	if (n == 1) return a[0];
	else if (n == 2) return a[0] > a[1] ? a[0] : a[1];
	else
	{
		int b[100],temp;
		for (int i = 1; i < n; i++)
			b[i - 1] = a[i];
		temp = max(b, n - 1);
		return a[0] > temp ? a[0] : temp;
	}
}

int main()
{
	int n,a[100];
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << max(a, n);
	return 0;
}
  1. 遞迴求陣列元素的和
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int sum(int a[], int n)
{
	switch (n)
	{
	case 0:
		return 0;
	case 1:
		return a[0];
	default:
		int b[100];
		for (int i = 0; i < n - 1; i++)
			b[i] = a[i];
		return a[n - 1] + sum(b, n - 1);
	}
}

int main()
{
	int n,a[100];
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << sum(a, n);
	return 0;
}
  1. 遞迴求Fibonacci序列的第n項
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int fib(int n)
{
	switch (n)
	{
	case 0:
		return 0;
	case 1:
		return 1;
	default:
		return fib(n - 1) + fib(n - 2);
	}
}

int main()
{
	int n;
	cin >> n;
	cout << fib(n);
	return 0;
}
  1. 遞迴逆序陣列元素
    在這裡插入圖片描述
#include "iostream"

using namespace std;

void reverse(int a[], int i, int n)//i是逆序的起始元素,n是終止元素
{
	if (n == 1||i==n)  return;//如果只有一個元素,或者起始元素與終止元素相同,不動
	if (n == 2)//如果有兩個元素,交換位置
	{
		int temp = a[0];
		a[0] = a[1];
		a[1] = temp;
		return;
	}
	else//如果多個元素,先將i到n-1逆序,並儲存第n個元素,然後將逆序後的陣列依次賦值給後面的數(陣列整體後移),然後將n元素賦值給i元素
	{
		int temp;
		temp = a[n-1];
		reverse(a, i , n-1);
		for (int j = n-1 ; j > 0; j--)
			a[j] = a[j-1];
		a[i-1] = temp;
		return;
	}
}

int main()
{
	int a[100],n,i;
	cin >> n;
	for ( i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	reverse(a, 1, n);
	for ( i = 0; i < n-1; i++)
	{
		cout << a[i]<<" ";
	}
	cout << a[i];
	return 0;
}
  1. 輸入輸出陣列元素的函式過載
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int input(int a[])
{
	int i = 0,temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	} 
}

int input(double a[])
{
	int i = 0;
	double temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	}
}

void print(int a[], int n)
{
	int i;
	for (i = 0; i < n - 1;i++)
	{
		cout << a[i]<<" ";
	}
	cout << a[i] << endl;
}

void print(double a[], int n)
{
	int i;
	for (i = 0; i < n - 1; i++)
	{
		cout << a[i] << " ";
	}
	cout << a[i] << endl;
}

int main()
{
	int ai[100];
	double ad[100];
	int ni, nd;
	ni = input(ai);
	nd = input(ad);
	print(ai, ni);
	print(ad, nd);
	return 0;
}
  1. 逆序函式過載
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int input(int a[])
{
	int i = 0,temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	} 
}

int input(double a[])
{
	int i = 0;
	double temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	}
}

void print(int a[], int n)
{
	int i;
	for (i = 0; i < n - 1;i++)
	{
		cout << a[i]<<" ";
	}
	cout << a[i] << endl;
}

void print(double a[], int n)
{
	int i;
	for (i = 0; i < n - 1; i++)
	{
		cout << a[i] << " ";
	}
	cout << a[i] << endl;
}

void reverse(int a[], int n)
{
	if (n == 1)  return;
	if (n == 2)
	{
		int temp = a[0];
		a[0] = a[1];
		a[1] = temp;
		return;
	}
	else
	{
		int temp;
		temp = a[n - 1];
		reverse(a, n - 1);
		for (int j = n - 1; j > 0; j--)
			a[j] = a[j - 1];
		a[0] = temp;
		return;
	}
}

void reverse(double a[], int n)
{
	if (n == 1)  return;
	if (n == 2)
	{
		double temp = a[0];
		a[0] = a[1];
		a[1] = temp;
		return;
	}
	else
	{
		double temp;
		temp = a[n - 1];
		reverse(a, n - 1);
		for (int j = n - 1; j > 0; j--)
			a[j] = a[j - 1];
		a[0] = temp;
		return;
	}
}

int main()
{
	int ai[100];
	double ad[100];
	int ni, nd;
	ni = input(ai);
	nd = input(ad);
	reverse(ai, ni);
	reverse(ad, nd);
	print(ai, ni);
	print(ad, nd);
	return 0;
}
  1. 陣列元素求和函式的過載
    在這裡插入圖片描述
#include "iostream"

using namespace std;

int input(int a[])
{
	int i = 0,temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	} 
}

int input(double a[])
{
	int i = 0;
	double temp;
	while (1)
	{
		cin >> temp;
		if (temp != -9999)
		{
			a[i] = temp;
			i++;
		}
		else return i;
	}
}

int mysum(int a[], int n)
{
	switch (n)
	{
	case 0:
		return 0;
	case 1:
		return a[0];
	default:
		int b[100];
		for (int i = 0; i < n - 1; i++)
			b[i] = a[i];
		return a[n - 1] + mysum(b, n - 1);
	}
}

double mysum(double a[], int n)
{
	switch (n)
	{
	case 0:
		return 0;
	case 1:
		return a[0];
	default:
		double b[100];
		for (int i = 0; i < n - 1; i++)
			b[i] = a[i];
		return a[n - 1] + mysum(b, n - 1);
	}
}

int main()
{
	int ai[100];
	double ad[100];
	int sumi;
	double sumd;
	int ni, nd;
	ni = input(ai);
	nd = input(ad);
	sumi = mysum(ai, ni);
	sumd = mysum(ad, nd);
	cout << sumi << " " << sumd << endl;
	return 0;
}
  1. 交換兩個元素值的過載函式
    在這裡插入圖片描述
    在這裡插入圖片描述
    在這裡插入圖片描述
#include "iostream"

using namespace std;

void swap(int &a, int &b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

void swap(double &a, double &b)
{
	double temp;
	temp = a;
	a = b;
	b = temp;
}

void swap(int a[], int &n, int b[], int &m)
{
	int max = n >= m ? n : m;
	for (int i = 0; i < max; i++)
		swap(a[i], b[i]);
	swap(n, m);
}


void swap(char a[], char b[])
{
	int i = 0,n;
	char temp;
	while (a[i] != '\0'&&b[i] != '\0')
	{
		temp = a[i];
		a[i] = b[i];
		b[i] = temp;
		i++;
	}
	n = i;
	if (a[i] == '\0')
	{
		while (b[i]!= '\0')
		{
			a[i] = b[i];
			i++;
		}
		a[i] = '\0';
		b[n] = '\0';
	}
	else
	{
		while (a[i] != '\0')
		{
			b[i] = a[i];
			i++;
		}
		b[i] = '\0';
		a[n] = '\0';
	}
}

void print(int a[], int n)
{
	int i;
	for (i = 0; i < n - 1; i++)
	{
		cout << a[i] << " ";
	}
	cout << a[i] << endl;
}

int main()
{
	int a, b;
	double da, db;
	int aa[100], ab[100];
	char s1[100], s2[100];
	int n, m;//n陣列aa的元素個數,m陣列ab元素個數
	int i;
	//輸入資料
	cin >> a >> b;
	cin >> da >> db;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> aa[i];
	}
	cin >> m;
	for (i = 0; i < m; i++)
	{
		cin >> ab[i];
	}
	cin >> s1 >> s2;

	//交換
	swap(a, b);
	swap(da, db);
	swap(aa, n, ab, m);
	swap(s1, s2);

	//輸出
	cout << a << " " << b << endl;
	cout << da << " " << db << endl;
	print(aa, n);
	print(ab, m);
	cout << s1 << " " << s2 << endl;
	
	return 0;

}

以上為第六週的基本練習。