1递归计算n!(10分)
题目内容:
编写递归函数,计算n!。在主函数中输入n,调用函数计算n!,在主函数中输出n!。
输入:非负整数。
输出:整数。
【注意】应用递归函数实现,否则没有意义。
样例1输入:
6
样例1输出:
720
时间限制:500
//递归计算n!
#include<iostream>
using namespace std;
int f(int n)
{
if (n == 1||n==0)
return 1;
else
return (n * f(n - 1));
}
int main()
{
int n;
cin >> n;
cout << f(n);
return 0;
}
2递归计算1+2+3+…+n(10分)
题目内容:
编写递归函数,计算1+2+3+…+n。在主函数中输入n,调用函数计算1到n的和,在主函数中输出和。
输入:非负整数。
输出:非负整数。
【注意】应用递归函数实现,否则没有意义。输入0时,和为0.
样例1输入:
10
样例1输出:
55
//递归计算1+2+3+…+n
#include<iostream>
using namespace std;
int f(int n)
{
if (n == 0 || n == 1)
return n;
else
return n + f(n - 1);
}
int main()
{
int n;
cin >> n;
cout << f(n) << endl;
return 0;
}
3递归求数组元素的最大值(10分)
题目内容:
编写递归函数,求数组元素的最大值,函数的输入参数为数组和元素个数,返回最大值。在主函数中输入元素个数和数组元素,调用函数求最大值,在主函数中输出结果。设数组类型为整型,元素不超过100个。
输入:元素个数n和n个元素,用空格或换行隔开。
输出:数组元素最大值。
【注意】使用递归,否则没有意义。
样例1输入:
6
12 1 2 3 14 6
样例1输出:
14
//递归求数组元素的最大值
#include<iostream>
using namespace std;
int mymax(int a[],int n)
{
if (n == 0)
return a[0];
int max = mymax(a, n - 1);
if (max < a[n - 1])
max = a[n - 1];
return max;
}
int main()
{
int n;
cin >> n;
int a[100];
int i;
for (i = 0; i < n; i++)
cin >> a[i];
cout << mymax(a, n) << endl;
return 0;
}
4递归求数组元素的和(10分)
题目内容:
编写递归函数,求数组元素的和,函数的输入参数为数组和元素个数,返回最大值。在主函数中输入元素个数和数组元素,调用函数求和,在主函数中输出结果。设数组类型为整型,元素不超过100个。
输入:元素个数n和n个元素,用空格或换行隔开。
输出:数组元素和。
【提示】元素个数为0时返回和是0.
样例1输入:
6
1 2 3 4 5 6
样例1输出:
21
//递归求数组元素的和
#include<iostream>
using namespace std;
int mysum(int a[], int n)
{
if (n == 0)
return 0;
else
return a[n-1] + mysum(a,n - 1);
}
int main()
{
int n;
int a[100];
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << mysum(a, n) << endl;
return 0;
}
5递归求Fibonacci序列的第n项(10分)
题目内容:
Fibonacci(费波那契)序列:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2),n=2,3,4,…
编写递归函数,计算Fibonacci序列的第n项(n=0,1,2,3,…)。在主函数中输入n,调用函数计算Fibonacci第n项,在主函数中输出结果。
输入:非负整数
输出:非负整数
【注意】应用递归函数实现。
样例1输入:
6
样例1输出:
8
//递归求Fibonacci序列的第n项
#include<iostream>
using namespace std;
int fibonacci(int n)
{
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n;
cin >> n;
cout << fibonacci(n);
return 0;
}
6递归逆序数组元素(10分)
题目内容:
编写递归函数,将数组元素逆序,函数的输入参数是数组、起始下标和元素个数。在主函数中输入元素个数和数组元素,调用函数逆序,在主函数中输出结果。设数组类型为整型,元素不超过100个。
输入:元素个数n和n个元素,用空格或换行隔开。
输出:逆序的数组元素,用一个空格隔开,末尾无空格。
【提示】本函数不需返回值。
【注意】必须用递归函数实现,否则没有意义。
样例1输入:
5
1 2 3 4 5
样例1输出:
5 4 3 2 1
//递归逆序数组元素
#include<iostream>
using namespace std;
void reverse(int a[], int left, int n)
{
if (n == 0 || n == 1)
return;
else
{
int temp = a[left];
a[left] = a[n+left-1];
a[n+left-1] = temp;
reverse(a, left + 1, n - 2);
}
}
int main()
{
int n;
cin >> n;
int a[100];
for (int i = 0; i < n; i++)
cin >> a[i];
reverse(a, 0, n);
cout << a[0];
for (int j = 1; j < n; j++)
cout << " " << a[j];
return 0;
}
7输入输出数组元素的函数重载(10分)
题目内容:
函数重载就是定义函数名相同,参数类型或参数个数不同的函数。这些函数称为重载函数。具有重载关系的函数一般具有相同或相似的功能,但作用的对象不同。
编写输入数组元素的重载函数,数组类型分别为int和double,输入-9999时结束输入,返回输入的元素个数。编写输出数组元素的重载函数,数组类型分别为int和double。编写主函数,定义数组,调用函数输入元素,调用函数输出元素。元素个数不超过100个。主函数如下:
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行为整型数组元素,第2行为double数组元素,均以-9999表示结束。
输出:两行,第1行为整型数组元素,第2行为double数组元素,以一个空格分隔,末尾无空格。
【提示】print函数中最后输出一个换行。
【注意】不使用重载,无意义。需提交完成程序。
样例1输入:
1 2 3 4 5 6 7 -9999
1.1 1.2 1.3 1.5 1.6 1.6 1.7 1.8 1.9 -9999
样例1输出:
1 2 3 4 5 6 7
1.1 1.2 1.3 1.5 1.6 1.6 1.7 1.8 1.9
//输入输出数组元素的函数重载
#include<iostream>
using namespace std;
int input(int a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999)
{
i++;
cin >> a[i];
}
return i;
}
int input(double a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999.0)
{
i++;
cin >> a[i];
}
return i;
}
void print(int a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << endl;
}
void print(double a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << 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;
}
8逆序函数重载(10分)
题目内容:
编写逆序数组元素的重载函数,数组元素类型分别为int和double。编写主函数,定义数组,可以调用上题的函数输入数组元素,调用逆序函数逆序数组元素,可以调用上题函数输出数组元素。注意,不能在逆序函数中输出。数组元素个数不超过100个。主函数如下:
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行为整型数组元素,第2行为double数组元素,均以-9999表示结束。
输出:两行,第1行为逆序的整型数组元素,第2行为逆序的double数组元素,以一个空格分隔,末尾无空格。
【提示】print函数中最后输出一个换行。
【注意】不使用重载,无意义。
样例1输入:
1 2 3 4 5 6 7 8 -9999
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 -9999
样例1输出:
8 7 6 5 4 3 2 1
1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1
//逆序函数重载
#include<iostream>
using namespace std;
int input(int a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999)
{
i++;
cin >> a[i];
}
return i;
}
int input(double a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999.0)
{
i++;
cin >> a[i];
}
return i;
}
void print(int a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << endl;
}
void print(double a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << endl;
}
void reverse(int a[], int n)
{
for (int i = 0; n > 1; i++)
{
int temp = a[i];
a[i] = a[n + i - 1];
a[n + i - 1] = temp;
n = n - 2;
}
}
void reverse(double a[], int n)
{
for (int i = 0; n > 1; i++)
{
double temp = a[i];
a[i] = a[n + i - 1];
a[n + i - 1] = temp;
n = n - 2;
}
}
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;
}
9数组元素求和函数的重载(10分)
题目内容:
编写数组元素求和的重载函数,数组元素类型分别为int和double。编写主函数,定义数组,可以调用前面的函数输入数组元素,调用求和函数求和,在主函数中输出结果。数组元素个数不超过100个。主函数如下:
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行为整型数组元素,第2行为double数组元素,均以-9999表示结束。
输出:一个整数和,一个实数和,用空格隔开。
【注意】不使用重载,无意义。
样例1输入:
1 2 3 4 5 6 7 8 9 10 -9999
1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 -9999
样例1输出:
55 13.5
//数组元素求和函数的重载
#include<iostream>
using namespace std;
int input(int a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999)
{
i++;
cin >> a[i];
}
return i;
}
int input(double a[])
{
int i = 0;
cin >> a[0];
while (a[i] != -9999.0)
{
i++;
cin >> a[i];
}
return i;
}
int mysum(int a[], int n)
{
int sum=0;
for (int i = 0; i < n; i++)
sum += a[i];
return sum;
}
double mysum(double a[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
sum += a[i];
return sum;
}
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;
}
10交换两个元素值的重载函数(10分)
题目内容:
编写交换两个元素值的重载函数,两个元素会是两个整数、两个实数、两个整型数组、两个char型字符串。编写主函数,定义有关变量、数组,输入数据,调用函数,输出交换结果。数组元素不超过100。主函数如下(其中print(是前面编写的显示数组元素的函数):
int main()
{
int a, b;
double da, db;
int aa[100], ab[100];
char s1[100], s2[100];
int n, m;
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;
return 0;
}
输入:5行;分别是两整数,两实数,数组1,数组2,两个字符串。见样例输入。其中数组行的第1个数是元素个数。
输出:5行,交换的结果,见样例输出。
【注意】不能使用系统的字符串处理库函数,不使用string。可以自定义需要的函数。
【提示】整数、实数的交换使用参数的引用传递;数组的交换,元素个数的交换使用引用传递。
样例1输入:
2 3
1.2 1.81
3 1 2 3
8 81 82 83 84 85 86 87 88
input output
样例1输出:
3 2
1.81 1.2
81 82 83 84 85 86 87 88
1 2 3
output input
//交换两个元素值的重载函数
#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;
if (max < m)
max = m;
for (int i = 0; i < max; i++)
swap(a[i], b[i]);//对数组进行交换
swap(n, m);//长度也进行交换
}
void swap(char a[], char b[])
{
char* p = a;
char* q = b;
while (*p||*q)
{
char temp = *q;
*q = *p;
*p = temp;
p++;
q++;
}
*q = '\0';
*p = '\0';
}
void print(int a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << endl;
}
void print(double a[], int n)
{
cout << a[0];
for (int i = 1; i < n; i++)
cout << " " << a[i];
cout << endl;
}
int main()
{
int a, b;
double da, db;
int aa[100], ab[100];
char s1[100], s2[100];
int n, m;
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;
return 0;
}