##第六周基本练习
本周内容为函数的递归以及重载
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
以上为第六周的基本练习。