/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年3月24日
*版本号:v1.0
*
*问题描述:写出求1*3*…*n的递归式,并编写出递归函数求解
*输入描述:输入最后一项n
*程序输出:n以内所有奇数的乘积
*/
问题及代码:
#include<iostream>
using namespace std;
int fun(int n);
int main()
{
int n;
cout<<"请输入一个数:"<<endl;
cin>>n;//输入数字n
if(n%2==0)//若n为偶数则求fun(n-1)
{
cout<<n<<"以内的奇数积是:"<<fun(n-1)<<endl;
}
else
cout<<fun(n)<<endl;
return 0;
}
int fun(int n)
{
if(n==1)
return 1;
else
return n*fun(n-2);//递归求乘积
}
运行结果:
知识点总结:
函数的递归。
学习心得:
注意判断所输入的数是奇数还是偶数。